在Unity中,我可以列出一些脚本吗?我有一个ModeController,它应该在运行时启用和禁用其他脚本。问题是它处理的脚本数量正在增加,手动启用和禁用每个脚本变得越来越困难。这是目前的代码:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
public class ModeController : MonoBehaviour {
public CharacterController characterController;
public CarController carController;
public JetpackController jetpackController;
public WhateverController1 whateverController1;
public WhateverController2 whateverController2;
public WhateverController3 whateverController3;
public WhateverController4 whateverController4;
void Start(){
SetMode ();
}
public void SetMode(){
if (mode == "Basic") {
characterController.enabled = true;
carController.enabled = false;
jetpackController.enabled = false;
whateverController1.enabled = false;
whateverController2.enabled = false;
whateverController3.enabled = false;
whateverController4.enabled = false;
} else if (mode == "Car") {
characterController.enabled = false;
carController.enabled = true;
jetpackController.enabled = false;
whateverController1.enabled = false;
whateverController2.enabled = false;
whateverController3.enabled = false;
whateverController4.enabled = false;
} else if (mode == "Jetpack") {
characterController.enabled = false;
carController.enabled = false;
jetpackController.enabled = true;
whateverController1.enabled = false;
whateverController2.enabled = false;
whateverController3.enabled = false;
whateverController4.enabled = false;
} else if (mode == "Whatever1") {
characterController.enabled = false;
carController.enabled = false;
jetpackController.enabled = false;
whateverController1.enabled = true;
whateverController2.enabled = false;
whateverController3.enabled = false;
whateverController4.enabled = false;
} else if (mode == "Whatever2") {
characterController.enabled = false;
carController.enabled = false;
jetpackController.enabled = false;
whateverController1.enabled = false;
whateverController2.enabled = true;
whateverController3.enabled = false;
whateverController4.enabled = false;
} else if (mode == "Whatever3") {
characterController.enabled = false;
carController.enabled = false;
jetpackController.enabled = false;
whateverController1.enabled = false;
whateverController2.enabled = false;
whateverController3.enabled = true;
whateverController4.enabled = false;
} else if (mode == "Whatever4") {
characterController.enabled = false;
carController.enabled = false;
jetpackController.enabled = false;
whateverController1.enabled = false;
whateverController2.enabled = false;
whateverController3.enabled = false;
whateverController4.enabled = true;
}
}
}
如何将所有这些脚本添加到一个列表中?类似的东西:
public List<SomeType> scripts = new List<SomeType>();
然后我可以遍历列表,如:
foreach(SomeType script in scripts){
if(something)
script.enabled = true;
else
script.enabled = false;
}
答案 0 :(得分:2)
Unity中的脚本扩展MonoBehaviour
,因此您可以执行以下操作
public List<MonoBehaviour> Scripts;
然后你可以像你说的那样循环播放
foreach(var script in Scripts)
{
script.enabled = true;
}
或者,您可以创建一个扩展Controller
的类MonoBehaviour
并使其具有WhateverController
子类。这将是控制器之间共享行为的一种方式。
答案 1 :(得分:0)
我最初在玩游戏时做了类似的事情,玩家必须能够在不同的模式之间切换(战斗,建筑等等)。
最终工作得更好的是使用子对象作为每组脚本的容器,然后通过将子对象设置为活动或非活动来切换子项。一旦你以这种方式设置它,就可以根据需要在子对象上交换脚本。
我还可以看到在这种情况下使用可编写脚本的对象。 Unity最近有一个关于使用您可能感兴趣的可编写脚本的对象的live training会话。
答案 2 :(得分:0)
public List<Component> components
可用于包含各种脚本。
将脚本添加到列表中并不容易:
WhateverController
,WhateverContoller
从第二个拖入第一个检查员。