How can I address a script I don't know the ty

2019-03-05 16:53发布

问题:

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.