c# - 如何在三元运算符中使用 void 函数(不同的参数)调用或在 1 行中编写 if-else
问题描述
我正在使用带有 c# 语言的 unity2019.2.0b。
当我创建对象并通过脚本更改对象的属性时,例如缩放旋转位置或速度
在我的情况下,使用 if-else 语句可以正常工作,但我想使用三元运算符,因为每个属性设置每个 1 行脚本以提高可读性。
所以我的问题是如何将 if else 语句转换为没有返回值函数的三元运算符
我想要的概念是检查可移动的真实与否
如果为真,则调用 function1(parameter1)
如果为假,则调用 function2(parameter1)
或者
如果为真,则调用 function3(parameter1)
如果为 false 则调用 function4(parameter1, parameter2)
所有函数都是没有返回值的函数。
我想这样写(movable 是布尔值)
movable? SetObjectMoving(gameobject) : RemoveComponent<MoveComponent>(gameobject);
这是使用 if-else 语句时的代码
if(movable)
{
ObjectMovingSystem(gameobject);
}else{
RemoveComponent<MoveComponent>(gameobject);
}
这是我的完整代码
private void CreateEntity()
{
for (int i = 0; i < spawnCountPerFrame; i++)
{
Entity instance = shapeFactory.GetEntityRandom();
entityManager.SetComponentData<Translation>(instance, new Translation { Value = SpawnZoneOfLevel.SpawnPoint });
entityManager.SetComponentData<Rotation>(instance, new Rotation { Value = Random.rotation });
entityManager.SetComponentData<Scale>(instance, new Scale { Value = Random.Range(0.1f, 1f) });
//(movable ? SetEntityMovement(instance) : RemoveMovementComponent<PersistantObjectMovement>(instance));
//PersistantObjectmovement
if (movable)
{
SetEntityMovement(instance);
}
else
{
RemoveMovementComponent<PersistantObjectMovement>(instance);
}
//(rotatable ? SetEntityRotation(instance) : RemoveMovementComponent<PersistantObjectRotation>(instance));
//PersistantObjectRotation
if (rotatable)
{
SetEntityRotation(instance);
}
else
{
RemoveMovementComponent<PersistantObjectRotation>(instance);
}
entityList.Add(instance);
}
}
private void SetEntityMovement(Entity instance)
{
entityManager.SetComponentData(instance, new PersistantObjectMovement
{
direction = Random.onUnitSphere,
speed = Random.Range(0.5f, 1)
});
}
private void SetEntityRotation(Entity instance)
{
entityManager.SetComponentData(instance, new PersistantObjectRotation
{
angularVelocity = Random.onUnitSphere * Random.Range(0f, 90f),
radiantPerSecond = math.radians(Random.Range(120f, 360f))
});
}
private void RemoveMovementComponent<T>(Entity instance) where T : IComponentData
{
entityManager.RemoveComponent(instance, typeof(T));
}
我知道这不是真的有必要做,但我想知道有没有做 if-else 语句像三元运算符一样写 1 行?
解决方案
@steve16351 给了我一个好主意。
使用三元运算符和 1 行我想要的。
Entity instance = shapeFactory.GetEntityRandom();
entityManager.SetComponentData<Translation>(instance, new Translation { Value = SpawnZoneOfLevel.SpawnPoint });
entityManager.SetComponentData<Rotation>(instance, new Rotation { Value = Random.rotation });
entityManager.SetComponentData<Scale>(instance, new Scale { Value = Random.Range(0.1f, 1f) });
(movable ? (Action<Entity>)SetEntityMovement : RemoveMovementComponent<PersistantObjectMovement>)(instance);
(rotatable ? (Action<Entity>)SetEntityRotation : RemoveMovementComponent<PersistantObjectRotation>)(instance);
你们觉得这段代码怎么样?
推荐阅读
- go - Golang 如何将网站部署到主机
- python - 获取python中函数的代码行数
- symfony - FormExtension 中的 PrestaShop ClassNotFoundException
- c# - xamarin android发送短信错误异常
- autohotkey - 如何在自动热键中使窗口成为焦点
- c++ - 如何可视化 C/C++ #define 指令?
- android - 设置 Android Studio 预览背景颜色
- nunit-3.0 - 将 NUnit3 XML 合并为一个 HTML 以发布到 Jenkins
- c# - HttpClient PostAsync 取消请求
- snakemake - snakemake + 奇点,摆脱python依赖