[C#] 纯文本查看 复制代码
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace NutUnityDebug
{
public delegate void gm_keydown_Action();
public class Nut : MonoBehaviour
{
public Nut(MonoBehaviour theMonoBehaviour)
{
this.MyMonoBehaviour = theMonoBehaviour;
}
//打印出当前的场景名字以及Unity版本
public static void NutDBG_showNowSencen()
{
UnityEngine.SceneManagement.Scene scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
Debug.Log(scene.buildIndex + "==NutDBG_showNowSencen==" + scene.name);
Debug.Log(Application.unityVersion + " ==Application.unityVersion==Application.version==" + Application.version);
}
//获取参数GameObject在场景中的路径
public static string NutDBG_getThePath(GameObject theGameObject)
{
string text = Nut.NutDBG_getThePath_temp(theGameObject, theGameObject.name);
Text component = theGameObject.GetComponent<Text>();
if (component != null)
{
text = text + "----Text->" + component.text;
}
return text;
}
private static string NutDBG_getThePath_temp(GameObject theGameObject, string returnPath)
{
Transform temp = theGameObject.transform.parent;
if (temp != null)
{
returnPath = temp.name + "/" + returnPath;
GameObject theTemp;
returnPath = NutDBG_getThePath_temp(temp.gameObject, returnPath);
}
return returnPath;
}
//按下对应的按键执行对应的功能
static float theConTime = 1;
public static void NutDBG_keydown(gm_keydown_Action temp)
{
theConTime -= Time.deltaTime;
if(theConTime < 0)
{
if (Input.GetKey(KeyCode.D))//按下D键
{
theConTime = 10;
NutDBG_showAll(); //把当前场景所有GameObject的名字显示在物体上
}
if (Input.GetKey(KeyCode.P))//按下P键
{
theConTime = 10;
NutDBG_showAllPath(); //显示当前场景所有GameObject的路径
}
if (Input.GetKey(KeyCode.S))//按下S键
{
theConTime = 10;
NutDBG_showNowSencen(); //打印出当前的场景名字以及Unity版本
}
}
}
//显示当前场景所有GameObject的路径
public static void NutDBG_showAllPath()
{
string text = "";
int thei = 0;
foreach (GameObject gameObject in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
{
MonoBehaviour.print(thei + "==NutDBG_showAllPath==>" + Nut.NutDBG_getThePath(gameObject));
thei++;
}
Debug.Log("-->" + text);
}
//把当前场景所有GameObject的名字显示在物体上
public static void NutDBG_showAll()
{
Debug.Log("NutDBG_showAll-------------->");
string text = "";
foreach (GameObject gameObject in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
{
if (gameObject.GetComponent<RectTransform>() != null)
{
GameObject gameObject2 = new GameObject("new=" + gameObject.name);
gameObject2.transform.parent = gameObject.transform;
gameObject2.transform.localPosition = new Vector3(0f, -30f + UnityEngine.Random.value * 60f, 0f);
gameObject2.AddComponent<Text>().text = "==" + gameObject.name;
gameObject2.GetComponent<Text>().color = new Color(1f, 0f, 1f);
gameObject2.GetComponent<Text>().fontSize = 12;
gameObject2.GetComponent<Text>().horizontalOverflow = HorizontalWrapMode.Overflow;
gameObject2.GetComponent<Text>().font = Font.CreateDynamicFontFromOSFont(Font.GetOSInstalledFontNames()[0], 20);
}
text = text + "|" + gameObject.transform.name;
}
Debug.Log("-->" + text);
}
// 获取Button上绑定的脚本,以及调用的方法名字
public static void NutDBG_getTheButtonScript(GameObject the_gameObject)
{
Button.ButtonClickedEvent onClick = the_gameObject.GetComponent<Button>().onClick;
for (int i = 0; i < onClick.GetPersistentEventCount(); i++)
{
Debug.Log(string.Concat(new object[]
{
"Ugui_getTheButtonScript-->",
onClick.GetPersistentTarget(i),
".",
onClick.GetPersistentMethodName(i)
}));
}
}
// 获取GameObject上绑定的所有脚本/组件
public static void NutDBG_getGameObjectScript(GameObject the_gameObject)
{
if(the_gameObject == null)
{
Debug.Log("Ugui_gm_getGameObjectScript-->fail-->no GameOBJ-->");
return;
}
Component[] components = the_gameObject.transform.GetComponents(typeof(Component));
for (int i = 0; i < components.Length; i++)
{
Debug.Log("Ugui_getGameObjectScript-->" + components[i]);
}
}
//向游戏中加入自己的图片
public GameObject NutDBG_create(string theName, Transform theParent, Vector3 theLocalPosition, string theUrl)
{
GameObject gameObject = new GameObject(theName);
gameObject.transform.parent = theParent;
gameObject.transform.localPosition = theLocalPosition;
gameObject.AddComponent<RawImage>();
this.MyMonoBehaviour.StartCoroutine(this.LoadTheImage(theUrl, gameObject));
return gameObject;
}
// 加载指定路径的图片
private IEnumerator LoadTheImage(string theUrl, GameObject theGameOBJ)
{
Debug.Log("传入的URL:" + theUrl);
WWW www = new WWW("jar:file://" + theUrl);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
Texture2D texture = www.texture;
theGameOBJ.GetComponent<RawImage>().texture = www.texture;
theGameOBJ.GetComponent<RawImage>().SetNativeSize();
}
yield break;
}
//向安卓层发消息 methodName为安卓层接收消息的方法名
public static void NutDBG_sendMsgToAndroid(string methodName, string msg = null)
{
Debug.Log("SendMsgToAndroid " + methodName + " and msg is " + msg);
using (AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
{
using (AndroidJavaObject [url=home.php?mod=space&uid=452844]@static[/url] = androidJavaClass.GetStatic<AndroidJavaObject>("currentActivity"))
{
if (msg == null)
{
@static.Call(methodName, new object[0]);
}
else
{
@static.Call(methodName, new object[]
{
msg
});
}
}
}
}
//NutDBG_sendMsgToAndroid() 这个方法的封装版本。。
//默认安卓层接收消息的方法名为u3dToJava_msg
//后面的两个string参数是为了自己方便输出的Log
public static void NutDBG_SendMessageToJava(string msg, string CallBackObjName, string CallBackMethodName)
{
try
{
if (msg != null)
{
Nut.NutDBG_sendMsgToAndroid("u3dToJava_msg", msg);
Debug.Log(string.Concat(new string[]
{
"SendMsg_msg ",
msg,
" Your CallBack Path is:(\"",
CallBackObjName,
"\",\"",
CallBackMethodName,
"\""
}));
}
else
{
Nut.NutDBG_sendMsgToAndroid("u3dToJava_exit", null);
Debug.Log(string.Concat(new string[]
{
"SendMsg_exit ",
msg,
" Your CallBack Path is:(\"",
CallBackObjName,
"\",\"",
CallBackMethodName,
"\""
}));
}
}
catch (Exception e)
{
if (e.ToString().Contains("UnityPlayerActivity"))
{
Debug.Log(string.Concat(new string[]
{
"SendMsg_Fail ",
msg,
" Your CallBack Path is:(\"",
CallBackObjName,
"\",\"",
CallBackMethodName,
"\""
}));
}
else
{
Debug.Log(string.Concat(new object[]
{
"NutDBG_sendMsgToAndroid=",
msg,
"=",
e
}));
}
}
}
//寻找该物体下所有包含Text组件的子物体
public static void NutDBG_GetTextFromChild(GameObject the_gameObject)
{
int Count = 0;
Transform[] AllObj = the_gameObject.GetComponentsInChildren<Transform>(true);
foreach (Transform child in AllObj)
{
if (child.gameObject.GetComponent<Text>() != null)
{
Debug.Log("Nut_Ugui__ --FatherName:" + the_gameObject.name + " ChildName:" + child.name + " Text:" + child.gameObject.GetComponent<Text>().text);
Count++;
}
}
if (Count == 0)
{
Debug.Log("Nut_Ugui__GetTextFromChild Sorry I Can't Find Text with this GameObj");
}
else
{
Debug.Log("Nut_Ugui__GetTextFromChild Query completed, " + Count.ToString() + " locations found");
}
}
//显示按钮的所有信息
//此方法加在UnityEngine.UI.Press()方法下 可打印出当前被点击按钮的所有信息
public static void NutDBG_ShowBtnInfo(GameObject Button)
{
Debug.Log("==UGUI=button=="+Button.name+"==localPosition=="+Button.transform.localPosition+"==anchorMin=="+Button.GetComponent<RectTransform>().anchorMin+"==anchorMax=="+Button.GetComponent<RectTransform>().anchorMax);
NutUnityDebug.Nut.NutDBG_getTheButtonScript(Button);
NutUnityDebug.Nut.NutDBG_getThePath(Button);
NutUnityDebug.Nut.NutDBG_getGameObjectScript(Button);
}
private MonoBehaviour MyMonoBehaviour;
}
}