List 脚本列表
在Unity中,我可以有一个脚本列表吗?我有一个ModeController,应该在运行时启用和禁用其他脚本。问题是它处理的脚本数量正在增加,手动启用和禁用每个脚本变得越来越困难。以下是目前的代码:List 脚本列表,list,unity3d,List,Unity3d,在Unity中,我可以有一个脚本列表吗?我有一个ModeController,应该在运行时启用和禁用其他脚本。问题是它处理的脚本数量正在增加,手动启用和禁用每个脚本变得越来越困难。以下是目前的代码: using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; public class ModeController : MonoBehaviour {
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>();
我最初在制作一个游戏时做了一些类似的事情,玩家必须能够在不同的模式(战斗、建筑等)之间切换 最终效果更好的方法是使用子对象作为每组脚本的容器,然后通过将子对象设置为活动或非活动来根据需要切换子对象。以这种方式进行设置后,可以根据需要在子对象上交换脚本
我还可以看到在这个场景中使用脚本对象。Unity最近举办了一次关于使用您可能感兴趣的可编写脚本的对象的会议 我最初在制作一个游戏时做了类似的事情,玩家必须能够在不同模式(战斗、建筑等)之间切换 最终效果更好的方法是使用子对象作为每组脚本的容器,然后通过将子对象设置为活动或非活动来根据需要切换子对象。以这种方式进行设置后,可以根据需要在子对象上交换脚本
我还可以看到在这个场景中使用脚本对象。Unity最近举办了一次关于使用您可能感兴趣的可编写脚本的对象的会议 Unity中的脚本扩展了MonoBehavior,因此您可以执行以下操作
public List<MonoBehaviour> Scripts;
或者,您可以创建一个类
Controller
扩展monobhavior
,并将WhateverController
子类化。这将是在控制器之间共享行为的一种方式。Unity中的脚本扩展了单行为,因此您可以执行以下操作
public List<MonoBehaviour> Scripts;
或者,您可以创建一个类Controller
扩展monobhavior
,并将WhateverController
子类化。这将是在控制器之间共享行为的一种方式。公共列表组件可用于包含各种脚本
不过,将脚本添加到列表中并不是那么容易:
选择包含列表的对象
锁上检视窗
打开第二个未锁定的检查器窗口(两者都需要可见)
选择包含您的WhateverController
的对象
将WhateverContoller
从第二个拖动到第一个检查器中
对每个要禁用的脚本从4开始重复
公共列表组件
可用于包含各种脚本
不过,将脚本添加到列表中并不是那么容易:
选择包含列表的对象
锁上检视窗
打开第二个未锁定的检查器窗口(两者都需要可见)
选择包含您的WhateverController
的对象
将WhateverContoller
从第二个拖动到第一个检查器中
对每个要禁用的脚本从4开始重复
注意:已启用
是单一行为
的属性。如果您使用组件
,您将无法访问它。注意:启用
是单一行为
的属性。如果您使用组件
,您将无法访问它。使用MonoBehavior是我一直在寻找的,它使我的生活更加轻松。谢谢。我一直在寻找使用MonoBehavior的方法,它让我的生活轻松了很多。非常感谢。
foreach(var script in Scripts)
{
script.enabled = true;
}