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

后端 未结 2 1060
予麋鹿
予麋鹿 2021-01-23 08: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

相关标签:
2条回答
  • 2021-01-23 08:18

    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
    
    0 讨论(0)
  • 2021-01-23 08:20

    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.

    0 讨论(0)
提交回复
热议问题