API

作者: en_young | 来源:发表于2019-05-23 00:32 被阅读0次

1、OnGUI():

官方文档描述:

OnGUI is called for rendering and handling GUI events.//函数被调用来渲染和处理GUI事件的。

This means that your OnGUI implementation might be called several times per frame (one call per event). If the MonoBehaviour's enabled property is set to false, OnGUI() will not be called.  //这意味着你的OnGUI实现可能每一帧被调用好多次(每一个事件调用一次)。如果你的MonoBehavior的enabled属性被设置为false,那么OnGUI函数将不会被调用。

例子:

using UnityEngine;

using System.Collections;

public class ExampleClass :MonoBehaviour{   

       void OnGUI()    {      

                   if (GUI.Button(newRect(10, 10, 150, 100), "I am a button"))        {   

               //public static bool Button(Rect position, string text);  当用户点击了按钮,就返回true;text是按钮上面的内容;position是:Rectangle on the screen to use for the button.

                              print("You clicked the button!");       

                    }   

         }}

Play Mode下的图为:

2、MonoBehaviour.OnRenderObject()函数:

Description

OnRenderObject is called after camera has rendered the Scene.  //摄像机渲染场景后,该函数被调用

This can be used to render your own objects using Graphics.DrawMeshNow or other functions. This function is similar to OnPostRender, except OnRenderObject is called on any object that has a script with the function; no matter if it's attached to a Camera or not

//这个可以用来渲染你自己的物体,结合着.Graphics.DrawMeshNow 函数或者其他的函数;这个函数和OnPostRender有点相似,除了OnRenderObject 能够被任何一个挂载含有该函数脚本的物体所调用,无论该物体是否与一个摄像机相连。

例子如下:

using System.Collections;

using UnityEngine;

public class ExampleClass :MonoBehaviour{   

     public  Mesh  mainMesh;   

     public  Mesh  miniMapMesh;

     void OnRenderObject()    { 

           // Render different meshes for the object depending on whether  the main camera or minimap camera is viewing.        

              if (Camera.current.name == "MiniMapcam")        {

                          Graphics.DrawMeshNow(miniMapMesh, transform.position, transform.rotation);        

               }        

               else        {

                            Graphics.DrawMeshNow(mainMesh, transform.position, transform.rotation);        

               }   

          }

}

相关文章

网友评论

      本文标题:API

      本文链接:https://www.haomeiwen.com/subject/irlmzqtx.html