How can I address a script I don't know the type of?

血红的双手。 提交于 2019-12-20 05:15:16

问题


My game uses a variety of different game modes, and I'd like to spawn a different GameController script at the beginning of the scene depending on the game mode selected. Then other items (e.g., Enemies), would reference the main GameController, whether that be GameController_Mode1, GameController_Mode2, etc. But how can I have other objects referencing this if I don't know the type?

Unity iOS requires strict unityscript typing, so I can't use duck typing to get around this.


回答1:


You can do this the same way you'd do it in C#, polymorphism. Derive all of your controllers from a single base Controller class. Then your GameController var can be set to any instantiation of a derived controller (see Start() in the example below).

Here is a quick example using a simple controller:

#pragma strict

var controller : MyController;

class MyController  {
var data : int;
public function MyController(){
    this.data = 42;
}
public function Print(){
    Debug.Log("Controller: " + this.data);
}
}
class MyController1 extends MyController {
public function MyController1(){
    this.data = 43;
}
public function Print(){
    Debug.Log("Controller1: " + this.data);
}
}
class MyController2 extends MyController {
public function MyController2(){
    this.data = 44;
}
public function Print(){
    Debug.Log("Controller2: " + this.data);
}
}

function Start () {
controller = new MyController();
controller.Print(); // prints Controller: 42

controller = new MyController1();
controller.Print(); // prints Controller1: 43

controller = new MyController2();
controller.Print(); // prints Controller2: 44
}

I'm making any assumption that your gamecontrollers share function names and that the only difference is the code in each function.

[Update]

Regarding Heisenbug's comment below: You can use GetComponent to get the base class controller if your controller is a component.

Baseclass(BaseController.js):

class BaseController extends MonoBehaviour{
    public function Print(){
        Debug.Log("BaseController");
    }
}

Extended class(Controller1.js):

class Controller1 extends BaseController {
    public function Print(){
        Debug.Log("Controller1: " + this.data);
    }
}

Test:

var controller : BaseController;
controller = gameObject.GetComponent("BaseController"); //.GetComponent(BaseController) also works
controller.Print(); // will print "Controller1" if actual attached component is a Controller1 type



回答2:


While it looks like there are some good answers already but it is worth mentioning Unity's SendMessage system. It is a really simple approach if all you need to do is call functions on the other object SendMessage.

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html

In short you can use the following syntax:

TargetGameObject.SendMessage("targetFunction", argument, SendMessageOptions.DontRequireReceiver);

You can also use SendMessage to call javascript functions from C# scripts or vice versa.



来源:https://stackoverflow.com/questions/16765211/how-can-i-address-a-script-i-dont-know-the-type-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!