在运行时更改变量的可访问性 [英] Change a variable's accessibility during runtime
问题描述
我想知道是否有空间在 Unity C# 中在运行时更改变量的可访问性?我想要这样做的原因是当我的脚本使用另一种状态时隐藏不必要的公共变量.我尝试了一些动态变量,但出现无法找到的错误.我有那个 springDistance
但我只想在 trapType
的状态是 springOut
时使用它.一些代码:
I wish to know if there is room for changing a variable's accessibility during runtime in Unity C#? The reason I want this is to hide unnecessary public variables when my script uses another state. I tried some dynamic variables, but I get errors that it can't be found.
I have that springDistance
but I want to use it only when the state of the trapType
is springOut
.
Some code:
public class SpringTrap : MonoBehaviour
{
private Transform objectToMove;
// Use this for initialization
public enum TypeOfTrap
{
springOut
}
[Tooltip("Set the type of trap here to use.")]
public TypeOfTrap trapType = TypeOfTrap.springOut;
public float springDistance;
void Start ()
{
objectToMove = transform.FindChild("ObjectsToAnimate");
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
//if the player has entered the danger zone
switch (trapType)
{
case TypeOfTrap.springOut:
break;
}
}
}
IEnumerator SpringOut(float springDistance)
{
float deltaDist = 0.0f;
while(deltaDist < springDistance)
{
objectToMove.position +=
}
}
}
推荐答案
检查器的操作是通过自定义编辑器脚本完成的.在那里你可以显示东西,例如基于条件.
Manipulation of the inspector is done with custom editor scripts. There you can display things, e.g. based on conditions.
这是一个与请求非常相似的示例(根据枚举字段显示不同的内容):http://answers.unity3d.com/questions/417837/change-inspector-variables-depending-on-enum.html
Here is an example that does a very similar thing to the requested (displaying different things based on an enum field): http://answers.unity3d.com/questions/417837/change-inspector-variables-depending-on-enum.html
这篇关于在运行时更改变量的可访问性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!