我在我的统一游戏中有一个门户。通过输入触发器,它应该加载一个场景。所以这段代码真的很短,这里是:Unity中的重构代码通过不使用检查员
public class PortalData : EnvironmentCommonData // the data class with some information
{
public PortalData(string sceneToLoad) // set the scene in the constructor
{
SceneToLoad = sceneToLoad;
}
public string SceneToLoad { get; set; } // the scene to load when entering
}
public class PortalController : EnvironmentCommonController // the portal class
{
[SerializeField]
string sceneToLoad; // set the scene in the inspector
private PortalData data;
private void Start()
{
data = new PortalData(sceneToLoad); // instance for the data class
}
private void OnTriggerEnter(Collider col) // player enters the trigger
{
if (CheckCollision(col, data.PlayerObject)) // is the entering object the player?
LoadScene(data.SceneToLoad); // load the new scene
}
}
所以我正在寻找一个聪明的方式不使用督察。我想通过脚本自动完成。
所以我试图设置控制器摘要,并创建了不同的控制器类继承这个控制器,但这不是优雅的。
在这里,你可以看到我尝试的例子:
https://hastebin.com/utuqubafuz.cs
是否有人有一个想法?
那么完美的剧本是这样的
public class Portal : Monobehaviour
{
string sceneToLoad = "";
void OnTriggerEnter(Collider col)
{
if(col.gameObject.tag == "Player")
LoadScene(sceneToLoad);
}
}
,但我不想检查,如果可能的话。
你必须在某处创建它们。如果你已经在场景中产生了它们,只需使用Inspector即可。如果你使用代码创建它们,那就是你要指定字符串的地方。 – Draco18s
你想从运行时产生'sceneToLoad'变量的值吗?如果是这样,你的场景名称是什么? – Programmer