Unity 基础 之 获取代码执行的当前 类、当前函数、当前代码行数、当前场景、当前挂载的游戏物体 等
目录
Unity 基础 之 获取代码执行的当前 类、当前函数、当前代码行数、当前场景、当前挂载的游戏物体 等
一、简单介绍
Unity中的一些基础知识点。
在游戏开发中,获取代码执行的当前 类、当前函数、当前代码行数、当前场景、当前挂载的游戏物体 等。
二、实现原理
1、使用Unity封装的接口,或者反射,得到对应的信息;
三、注意事项
1、根据自己的需要,选择需要的信息;
四、效果预览
五、关键代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class TestScripts : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Init();
Show(this.gameObject);
}
void Init()
{
//非静态函数版本
string CallStack = "CallStack:[" + new System.Diagnostics.StackTrace().GetFrame(1).GetMethod() + "] => ";
string SceneName = " Scene:[" + SceneManager.GetActiveScene().name + "]";
string GameObjectName = " GameObject:[" + gameObject.name + "]";
string ClassName = " Class:[" + this.GetType().Name + "]";
string FunctionName = " Function:[" + System.Reflection.MethodBase.GetCurrentMethod().Name + "]";
string Log = " Log:[" + "runing" + "]";
string OutputResult = CallStack + SceneName + GameObjectName + ClassName + FunctionName + Log;
Debug.Log(OutputResult);
}
public static void Show(GameObject go)
{
//静态函数版本
string callStack = "CallStack:[" + new System.Diagnostics.StackTrace().GetFrame(1).GetMethod() + "] => ";
string FileName = " FileName:[" + System.IO.Path.GetFileName(new System.Diagnostics.StackTrace(1, true).GetFrame(0).GetFileName()) + "]";
string LineNumber = " Line:[" + new System.Diagnostics.StackTrace(1, true).GetFrame(0).GetFileLineNumber().ToString() + "]";
string sceneName = " Scene:[" + SceneManager.GetActiveScene().name + "]";
string gameObjectName = " GameObject:[" + go.name + "]";
string className = " Class:[" + System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName + "]";
string functionName = " Function:[" + System.Reflection.MethodBase.GetCurrentMethod().Name + "]";
string log = " Log:[" + "GetComponentNullLog" + "]";
string OutputResult = callStack + FileName + LineNumber + sceneName + gameObjectName + className + functionName + log;
Debug.Log(OutputResult);
}
}