首页 > 解决方案 > 如何在三元运算符中使用 void 函数(不同的参数)调用或在 1 行中编写 if-else

问题描述

我正在使用带有 c# 语言的 unity2019.2.0b。

当我创建对象并通过脚本更改对象的属性时,例如缩放旋转位置或速度

在我的情况下,使用 if-else 语句可以正常工作,但我想使用三元运算符,因为每个属性设置每个 1 行脚本以提高可读性。

所以我的问题是如何将 if else 语句转换为没有返回值函数的三元运算符

我想要的概念是检查可移动的真实与否

或者

所有函数都是没有返回值的函数。

我想这样写(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 行?

标签: c#unity3dif-statementdelegatesaction

解决方案


@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);

你们觉得这段代码怎么样?


推荐阅读