unity3d+unity_android+宏,unity3d DefineManager 全局宏定义

这个博客介绍了一个用于管理Unity项目中不同编译器全局宏定义的编辑器窗口脚本。通过该脚本,开发者可以方便地查看和编辑C#、UnityScript、Boo以及编辑器的宏定义,支持添加、删除和应用修改。这对于优化编译过程和针对不同平台进行条件编译非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**

*Editor Wizard for easily managing global defines in Unity

*Place in Assets/Editor folder, or if you choose to place elsewhere

*be sure to also modify the DEF_MANAGER_PATH constant.

*@khenkel

*/

using UnityEngine;

using UnityEditor;

using System.IO;

using System.Collections;

using System.Collections.Generic;

using System.Text;

public class DefineManager : EditorWindow

{

const string DEF_MANAGER_PATH = "Assets/Editor/DefineManager.cs";

enum Compiler

{

CSharp,

Editor,

UnityScript,

Boo

}

Compiler compiler = Compiler.Editor;

// http://forum.unity3d.com/threads/93901-global-define/page2

// Do not modify these paths

const int COMPILER_COUNT = 4;

const string CSHARP_PATH = "Assets/smcs.rsp";

const string EDITOR_PATH = "Assets/gmcs.rsp";

const string UNITYSCRIPT_PATH = "Assets/us.rsp";

const string BOO_PATH = "Assets/boo.rsp";

List csDefines = new List();

List booDefines = new List();

List usDefines = new List();

List editorDefines = new List();

[MenuItem("Window/Define Manager")]

public static void OpenDefManager()

{

EditorWindow.GetWindow(true, "Global Define Manager", true);

}

void OnEnable()

{

csDefines = ParseRspFile(CSHARP_PATH);

usDefines = ParseRspFile(UNITYSCRIPT_PATH);

booDefines = ParseRspFile(BOO_PATH);

editorDefines = ParseRspFile(EDITOR_PATH);

}

List defs;

Vector2 scroll = Vector2.zero;

void OnGUI()

{

Color oldColor = GUI.backgroundColor;

GUILayout.BeginHorizontal();

for(int i = 0; i < COMPILER_COUNT; i++)

{

if(i == (int)compiler)

GUI.backgroundColor = Color.gray;

GUIStyle st;

switch(i)

{

case 0:

st = EditorStyles.miniButtonLeft;

break;

case COMPILER_COUNT-1:

st = EditorStyles.miniButtonRight;

break;

default:

st = EditorStyles.miniButtonMid;

break;

}

if(GUILayout.Button( ((Compiler)i).ToString(), st))

compiler = (Compiler)i;

GUI.backgroundColor = oldColor;

}

GUILayout.EndHorizontal();

switch(compiler)

{

case Compiler.CSharp:

defs = csDefines;

break;

case Compiler.Editor:

defs = editorDefines;

break;

case Compiler.UnityScript:

defs = usDefines;

break;

case Compiler.Boo:

defs = booDefines;

break;

}

GUILayout.Label(compiler.ToString() + " User Defines");

scroll = GUILayout.BeginScrollView(scroll);

for(int i = 0; i < defs.Count; i++)

{

GUILayout.BeginHorizontal();

defs[i] = EditorGUILayout.TextField(defs[i]);

GUI.backgroundColor = Color.red;

if(GUILayout.Button("x", GUIStyle.none, GUILayout.MaxWidth(18)))

defs.RemoveAt(i);

GUI.backgroundColor = oldColor;

GUILayout.EndHorizontal();

}

GUILayout.Space(4);

GUI.backgroundColor = Color.cyan;

if(GUILayout.Button("Add"))

defs.Add("NEW_DEFINE");

GUILayout.EndScrollView();

GUILayout.BeginHorizontal();

GUI.backgroundColor = Color.green;

if( GUILayout.Button("Apply") )

{

SetDefines(compiler, defs);

AssetDatabase.ImportAsset(DEF_MANAGER_PATH, ImportAssetOptions.ForceUpdate);

OnEnable();

}

GUI.backgroundColor = Color.red;

if(GUILayout.Button("Apply All", GUILayout.MaxWidth(64)))

for(int i = 0; i < COMPILER_COUNT; i++)

{

SetDefines((Compiler)i, defs);

AssetDatabase.ImportAsset(DEF_MANAGER_PATH, ImportAssetOptions.ForceUpdate);

OnEnable();

}

GUILayout.EndHorizontal();

GUI.backgroundColor = oldColor;

}

void SetDefines(Compiler compiler, List defs)

{

switch(compiler)

{

case Compiler.CSharp:

WriteDefines(CSHARP_PATH, defs);

break;

case Compiler.UnityScript:

WriteDefines(UNITYSCRIPT_PATH, defs);

break;

case Compiler.Boo:

WriteDefines(BOO_PATH, defs);

break;

case Compiler.Editor:

WriteDefines(EDITOR_PATH, defs);

break;

}

}

List ParseRspFile(string path)

{

if(!File.Exists(path))

return new List();

string[] lines = File.ReadAllLines(path);

List defs = new List();

foreach(string cheese in lines)

{

if(cheese.StartsWith("-define:"))

{

defs.AddRange( cheese.Replace("-define:", "").Split(‘;‘) );

}

}

return defs;

}

void WriteDefines(string path, List defs)

{

if(defs.Count < 1 && File.Exists(path))

{

File.Delete(path);

if(File.Exists(path + ".meta"))

File.Delete(path + ".meta");

AssetDatabase.Refresh();

return;

}

StringBuilder sb = new StringBuilder();

sb.Append("-define:");

for(int i = 0; i < defs.Count; i++)

{

sb.Append(defs[i]);

if(i < defs.Count-1) sb.Append(";");

}

using (StreamWriter writer = new StreamWriter(path, false))

{

writer.Write(sb.ToString());

}

}

}

原文:http://www.cnblogs.com/j349900963/p/4476538.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值