简体   繁体   English

当我未在代码中调用它时,为什么我的方法被Unity调用?

[英]Why is my method called by Unity when I don't call it in code?

I can not understand why the method OnDrawGizmos() executes while it is not called in both the Start() method and Update() method, but it is executed when I run the Unity project. 我不明白为什么在Start()Update()方法中都没有调用OnDrawGizmos()方法时执行该方法,但是在我运行Unity项目时却执行了该方法。

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{
    public Transform grounder;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void OnDrawGizmos(){
        Gizmos.color = Color.white;
        Gizmos.DrawSphere (grounder.position, 1);
    }
}

As the name suggests, OnDrawGizmozs gets called by the unity engine. 顾名思义,统一引擎调用OnDrawGizmozs It's a message function that gets called on classes that implement MonoBehaviour. 这是一个消息函数,可在实现MonoBehaviour的类上调用。 Excert from the documentation: 来自文档的证明:

Description 描述

Implement OnDrawGizmos if you want to draw gizmos that are also pickable and always drawn. 如果要绘制也可拾取且始终绘制的小控件,请实现OnDrawGizmos。

http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDrawGizmos.html http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDrawGizmos.html

This means you don't need to call the function yourself. 这意味着您不需要自己调用该函数。

根据文档 ,因为它是由Unity在实现MonoBehaviour的类上调用的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我调用方法时,为什么不调用覆盖的方法? - When I call a method, why isn't the overridden method called? 为什么在运行计时器以回调该方法时脚本会挂起? Unity C# - Why Does My Script Hang When I Run The Timer To Call Back The Method ? Unity C# 出于某种原因,我的代码不应该在我的僵尸中产生,我不知道为什么 - For some reason my code is spawning in my zombies when it is not supposed to and I don't know why 输入名称时,我的代码将返回StackOverFlowException ...我不明白为什么 - My code returns a StackOverFlowException when I enter a name… I don't understand why 统一地,当我调用静态方法时,程序不起作用 - in unity, when i call static method, program doesn't work 当我用其他方法更改控件时,为什么不更新控件? - Why don't my controls update when I change them in another method? Unity 要求一个 } 但我不明白为什么 - Unity is asking for a } but I don't see why 为什么当我使用RegisterStartupScript或RegisterClientScriptBlock时,我的C#代码不起作用? - Why when I use RegisterStartupScript or RegisterClientScriptBlock my C# code don't works? 当我实际上未在代码中进行更改时,为什么椭圆的不透明度会自动更改? - Why is the opacity of my ellipse changing automatically when I don't actually change it in the code? 为什么我不能在视图的演示者背后的视图代码中调用方法? - Why can't I call a method in my View's code-behind from the view's presenter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM