unity 工具开发基础

时间:2023-03-10 02:35:37
unity 工具开发基础
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEditor;
/// <summary>
/// Unity基础工具功能
/// priority菜单项显示排序
/// </summary>
public class TestEditor : MonoBehaviour { [MenuItem("GameObject/获取选中的物体名称", priority = )]
static string GetSelectObject(){
GameObject _selectObject = Selection.gameObjects [];
Debug.Log (_selectObject.name);
return _selectObject.name;
} [MenuItem("GameObject/获取Project视图中的文件和文件夹路径", priority = )]
static string GetSelectFile(){
string[] guidArray = Selection.assetGUIDs;//从meta文件里面取得id
string selectName = AssetDatabase.GUIDToAssetPath (guidArray []);//根据id取得路径
string path = Application.dataPath.Substring (, Application.dataPath.Length - );
Debug.Log (path + selectName);
return path + selectName;
} [MenuItem("GameObject/创建新文件", priority = )]
static void CreateNewFile(){
File.WriteAllText (GetSelectFile () + "/UI_" + GetSelectObject () + ".lua", "xxx文本内容xxx", Encoding.UTF8);
AssetDatabase.Refresh ();
} [MenuItem("GameObject/创建文件夹", priority = )]
static void CreateFolder(){
Directory.CreateDirectory(Application.dataPath + "/文件夹名称");
AssetDatabase.Refresh ();
}
}

放Editor文件下

editor修改prefab    https://cnblogs.com/klkucan/p/4934518.html

-10-23新增

    [MenuItem("GameObject/获取用户名和当前时间", priority = )]
static void GetUserName(){
string username = System.Environment.UserName;
var time = System.DateTime.Now;
Debug.Log (username);
Debug.Log (time);
} [MenuItem("GameObject/打印选择物体的子物体路径", priority = )]
static void GetChildPath()
{
Transform _selectObject = Selection.gameObjects[].transform;
GetChildObject (_selectObject, _selectObject.name);
} static void GetChildObject(Transform trans, string path){
for(int i = ; i < trans.childCount; i++){
Transform child = trans.GetChild (i);
string comPath = path + "_" + child.name;
GetChildObject (child, comPath);
}
//输出条件:最末端物体 (*更改)
if(trans.childCount == ){
Debug.Log (path);
}
}

打印 路径 和 名字

    static void CreateUI(){
string[] guidArray = Selection.assetGUIDs;
foreach (var item in guidArray)
{
//打印路径
string selecetFloder = AssetDatabase.GUIDToAssetPath(item);
Debug.Log(selecetFloder);
//打印名字
string selectName = Path.GetFileName(selecetFloder);
Debug.Log(selectName);
}
}

遍历文件夹下的包括子文件夹的后缀带有xx文件

以lua为例

    [MenuItem("Assets/check",priority = )]
static void CheckChinese()
{
string[] guidArray = Selection.assetGUIDs;
foreach (var item in guidArray)
{
string selectFloder = AssetDatabase.GUIDToAssetPath(item);
DirectoryInfo root = new DirectoryInfo(selectFloder);
GetFloder(root);
}
} static void GetFloder(DirectoryInfo root)
{
GetFile(root);
//查找子文件夹
DirectoryInfo[] array = root.GetDirectories();
//Debug.Log(root);
foreach (DirectoryInfo item in array)
{
GetFloder(item);
}
} static void GetFile(DirectoryInfo root)
{
//DirectoryInfo root = new DirectoryInfo(path);
FileInfo[] fileDic = root.GetFiles();
foreach (var file in fileDic)
{
//sDebug.Log(file);
if (file.FullName.EndsWith("lua"))
{
Debug.Log("-------------" + file.Name);
}
}
}
//获取指定类型资源的路径
string[] spritesPath =
AssetDatabase.FindAssets("t:Sprite", new string[] {Application.dataPath + "/Resources/Sprites/Cloud"}); --------------------------------2019-8-28------------------------------------
代码设置Animator的参数,animator 转 AnimatorController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Spine.Unity;
using UnityEditor.Animations; /// <summary>
/// 制作怪物预制体
/// </summary>
public class CreateMonsterPrefab { [MenuItem("GameObject/添加怪物Animator参数", priority = )]
static void SetACPara()
{
Debug.Log("设置参数");
GameObject _selectObject = Selection.gameObjects[];
AnimatorController controller = _selectObject.GetComponent<Animator>().runtimeAnimatorController as AnimatorController; AddParameter(controller, "Die", AnimatorControllerParameterType.Bool);
AddParameter(controller, "Move", AnimatorControllerParameterType.Bool);
AddParameter(controller, "Hurt", AnimatorControllerParameterType.Bool);
AddParameter(controller, "IsDead", AnimatorControllerParameterType.Bool);
AddParameter(controller, "Speed", AnimatorControllerParameterType.Float);
AddParameter(controller, "Vertigo", AnimatorControllerParameterType.Bool);
AddParameter(controller, "Execute", AnimatorControllerParameterType.Bool);
AddParameter(controller, "IsMoveModeEnable", AnimatorControllerParameterType.Bool);
AddParameter(controller, "Frozen", AnimatorControllerParameterType.Bool);
AddParameter(controller, "Warning", AnimatorControllerParameterType.Bool);
AddParameter(controller, "IsWarning", AnimatorControllerParameterType.Trigger);
AddParameter(controller, "IsAtk", AnimatorControllerParameterType.Bool);
AddParameter(controller, "Attack", AnimatorControllerParameterType.Int);
AddParameter(controller, "AtkType", AnimatorControllerParameterType.Int);
AddParameter(controller, "MoveType", AnimatorControllerParameterType.Int);
AddParameter(controller, "AttackType", AnimatorControllerParameterType.Int); Debug.Log("设置成功");
} //添加
static void AddParameter(AnimatorController controller, string name, AnimatorControllerParameterType _type)
{
if (!CheckParameters(controller, name))
{
controller.AddParameter(name, _type);
}
} //检查
static bool CheckParameters(AnimatorController controller, string parName)
{
foreach (var item in controller.parameters)
{
if (item.name.Equals(parName))
{
return true;
}
}
return false;
}
}
不错的教程:
https://blog.****.net/q764424567/article/details/80908614