C# 如何使用void函数(不同的参数)调用三元运算符或在一行中写入if-else
我正在使用带有c#langauge的unity2019.2.0b 当我创建对象并通过脚本(如缩放旋转位置或速度)更改对象的属性时 在我的例子中,if-else语句可以很好地工作,但我想使用三元运算符,因为每个属性都设置为每1行脚本,以提高可读性 所以我的问题是如何将if-else语句转换成三元运算符,它没有返回值函数 我想要的概念是检查movable是否为真C# 如何使用void函数(不同的参数)调用三元运算符或在一行中写入if-else,c#,unity3d,if-statement,delegates,action,C#,Unity3d,If Statement,Delegates,Action,我正在使用带有c#langauge的unity2019.2.0b 当我创建对象并通过脚本(如缩放旋转位置或速度)更改对象的属性时 在我的例子中,if-else语句可以很好地工作,但我想使用三元运算符,因为每个属性都设置为每1行脚本,以提高可读性 所以我的问题是如何将if-else语句转换成三元运算符,它没有返回值函数 我想要的概念是检查movable是否为真 如果为true,则调用函数1(参数1) 如果为false,则调用函数2(参数1) 或 如果为true,则调用函数3(参数1) 如果为
- 如果为true,则调用函数1(参数1)
- 如果为false,则调用函数2(参数1)
- 如果为true,则调用函数3(参数1)
- 如果为false,则调用函数4(参数1、参数2)
可移动?SetObjectMoving(游戏对象):RemoveComponent(游戏对象);
这是使用if-else语句时的代码
if(movable)
{
ObjectMovingSystem(gameobject);
}else{
RemoveComponent<MoveComponent>(gameobject);
}
if(可移动)
{
ObjectMovingSystem(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));
}
private void CreateEntity()
{
for(int i=0;i
我知道这不是真的需要做的,但我想知道如果else语句像三元运算符一样写一行,还有什么需要做的?我想这符合条件:p
if (movable) SetEntityMovement(instance); else RemoveMovementComponent<PersistantObjectMovement>(instance);
if(可移动)SetEntityMovement(实例);else RemoveMovementComponent(实例);
我不认为有一个三元风格的操作符。我可能只会用普通的if,或者用大括号。我想你指的是三元运算符,而不是三元运算符 三元运算符用于返回值,如果不使用它们返回值,则是滥用它们。换句话说,如果有人阅读您的代码,看到一个三元运算符,他可以期望该操作返回一个值 如果使用三元运算符而不是If语句只是有条件地执行代码,则会使代码的可读性降低,而不是提高
这不起作用的原因是因为三元运算符需要知道返回类型才能编译。它通过查看“?”后面的子句的返回类型来实现这一点。void方法没有返回类型,因此三元运算符的编译将无法获取返回类型 @steve16351给了我一个好主意 使用三元运算符和我想要的一行
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);
Entity instance=shapeFactory.getEntityRoom();
SetComponentData(实例,新翻译{Value=spawnZoneOfflevel.SpawnPoint});
entityManager.SetComponentData(实例,新旋转{Value=Random.Rotation});
entityManager.SetComponentData(实例,新刻度{Value=Random.Range(0.1f,1f)});
(movable?(Action)SetEntityMovement:RemoveMovementComponent)(实例);
(可旋转的?(操作)SetEntityRotation:RemoveMovementComponent(实例);
你们觉得用这段代码怎么样?如果你们真的想这么做,可以使用:
(movable ? (Action<bool>)SetEntityMovement : RemoveMovementComponent<PersistantObjectMovement>)(instance)
(movable?(Action)SetEntityMovement:RemoveMovementComponent)(实例)
世上没有“三元”运算符,我认为你做不到。最近的是if(可移动的)X();else Y()那是我的错。我不知道我为什么写三元数。。。它的三元运算符可能会有帮助。。然而,这是错误地使用了操作符,并没有比你所拥有的更好。我已经读过这个案例了。当函数1和2具有相同的参数时,这种情况是好的,因此操作将对其起作用。但我在我的案子里试过了,但没用……谢谢。这对我来说并不完美,但对我来说是一个有用的答案。我的错,它是三元运算符,就像你说的。所以我不能在我的例子中使用三元运算符..你可以破解它,但这不是一个好主意,你会使代码的可读性大大降低。另一方面,代码可读性实际上不是指代码行数少,而是指理解能力强
(movable ? (Action<bool>)SetEntityMovement : RemoveMovementComponent<PersistantObjectMovement>)(instance)