Unity3D常用代码总结
1 GUI汇总
function OnGUI() {
GUI.Label(Rect(1,1,100,20),"I'm a Label"); //1
GUI.Box(Rect(1,21,100,20),"I'm a Box"); //2
GUI.Button(Rect(1,41,100,20),"I'm a Button"); //3
GUI.RepeatButton(Rect(1,61,120,20),"I'm a RepeatButton"); //4
GUI.TextField(Rect(1,81,100,20),"I'm a TextFielld"); //5
GUI.TextArea(Rect(1,101,100,40),"I'm a TextArea,\nMultiline"); //6
GUI.Toggle(Rect(1,141,120,20),true,"I'm a Toggle true"); //7
GUI.Toggle(Rect(1,161,120,20),false,"I'm a Toggle false"); //8
GUI.Toolbar(Rect(1,181,160,20),-1,["Toolbar","Tool2","Tool3"); //9
GUI.SelectionGrid(Rect(1,201,190,20),2,["Selection","Grid","select3"],3); //10
GUI.HorizontalSlider(Rect(1,221,180,20),3.0,0,10.0); //11
GUI.VerticalScrollbar(Rect(1,241,20,100),3.0,1,0.0,10.0); //12
//13
GUI.BeginScrollView (Rect (200,10,100,100),Vector2.zero, Rect (0, 0, 220, 200));
GUI.Label(Rect(0,0,100,20),"I'm a Label");
GUI.EndScrollView();
//14
GUI.Window(0,Rect(200,129,100,100),funcwin,"window");
}
function funcwin(windowID:int)
{
GUI.DragWindow(Rect(0,0,10000,2000));
}
2 JS调用DLL
import System;
import System.Runtime.InteropServices;
@DllImport("user32.dll")
public static function MessageBox(Hwnd : int,text : String,Caption : String,iType : int) : int {};
function Start()
{
MessageBox(0, "API Message Box", "Win32 API", 64) ;
}
function Update () {
}
//Declare our list of stuff
public List<GuiListItem> MyListOfStuff;
// Initialization
void Start()
{
guiWidth = 400;
guiHight = 28;
// Manually position our list, because the dropdown will appear over other controls
DropDownRect = new Rect(10, 10, guiWidth, guiHight);
DropdownVisible = false;
itemtSelected = -1;
targetChange = false;
lastCaption = selectedItemCaption = "Select a Part...";
if (!root)
root = gameObject.transform;
MyListOfStuff = new List<GuiListItem>(); //Initialize our list of stuff
// fill the list
BuildList(root);
// set GUI for each item in list
SetupGUISetting();
// fill the list
FillList(root);
}
void OnGUI()
{
//Show the dropdown list if required (make sure any controls that should appear behind the list are before this block)
if (DropdownVisible)
{
GUI.SetNextControlName("ScrollView");
GUILayout.BeginArea(new Rect(DropDownRect.xMin, DropDownRect.yMin + DropDownRect.height, guiWidth, Screen.height * .25f), "", "box");
ListScrollPos = GUILayout.BeginScrollView(ListScrollPos, dropSkin.scrollView);
GUILayout.BeginVertical(GUILayout.Width(120));
for (int i = 0; i < MyListOfStuff.Count; i++)
{
if (MyListOfStuff[i].Selected && GUILayout.Button(MyListOfStuff[i].Name, dropSkin.customStyles[MyListOfStuff[i].GuiStyle]))
{
HandleSelectedButton(i);
}
}
GUILayout.EndVertical();
GUILayout.EndScrollView();
GUILayout.EndArea();
}
//Draw the dropdown control
GUILayout.BeginArea(DropDownRect, "", "box");
GUILayout.BeginHorizontal();
string ButtonText = (DropdownVisible) ? "<<" : ">>";
DropdownVisible = GUILayout.Toggle(DropdownVisible, ButtonText, "button", GUILayout.Width(32), GUILayout.Height(20));
GUI.SetNextControlName("PartSelect");
selectedItemCaption = GUILayout.TextField(selectedItemCaption);
clearDropList = GUILayout.Toggle(clearDropList, "Clear", "button", GUILayout.Width(40), GUILayout.Height(20));
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
void Update()
{
//check if text box info changed
if (selectedItemCaption != lastCaption)
{
textChanged = true;
}
// if text box info changed look for part matching text
if (textChanged)
{
lastCaption = selectedItemCaption;
textChanged = false;
// go though list to find item
for (int i = 0; i < MyListOfStuff.Count; ++i)
{
if (MyListOfStuff[i].Name.StartsWith(selectedItemCaption, System.StringComparison.CurrentCultureIgnoreCase))
{
MyListOfStuff[i].enable();
MyListOfStuff[i].ToggleChildren = false;
MyListOfStuff[i].GuiStyle = MyListOfStuff[i].UnSelectedStyle;
}
else
{
MyListOfStuff[i].disable();
MyListOfStuff[i].ToggleChildren = false;
MyListOfStuff[i].GuiStyle = MyListOfStuff[i].UnSelectedStyle;
}
}
for (int i = 0; i < MyListOfStuff.Count; ++i)
{
// check list for item
int test = string.Compare(selectedItemCaption, MyListOfStuff[i].Name, true);
if (test == 0)
{
itemtSelected = i;
targetChange = true;
break; // stop looking when found
}
}
}
// reset message if list closed and text box is empty
if (selectedItemCaption == "" && !DropdownVisible)
{
lastCaption = selectedItemCaption = "Select a Part...";
ClearList(root);
FillList(root);
}
// if Clear button pushed
if (clearDropList)
{
clearDropList = false;
selectedItemCaption = "";
}
}
public void HandleSelectedButton(int selection)
{
// do the stuff, camera etc
itemtSelected = selection;//Set the index for our currently selected item
updateInfo = true;
selectedItemCaption = MyListOfStuff[selection].Name;
currentRoot = GameObject.Find(MyListOfStuff[itemtSelected].Name).transform;
// toggle item show child
MyListOfStuff[selection].ToggleChildren = !MyListOfStuff[selection].ToggleChildren;
lastCaption = selectedItemCaption;
// fill my drop down list with the children of the current selected object
if (!MyListOfStuff[selection].ToggleChildren)
{
if (currentRoot.childCount > 0)
{
MyListOfStuff[selection].GuiStyle = MyListOfStuff[selection].SelectedStyle;
}
FillList(currentRoot);
}
else
{
if (currentRoot.childCount > 0)
{
MyListOfStuff[selection].GuiStyle = MyListOfStuff[selection].UnSelectedStyle;
}
ClearList(currentRoot);
}
targetChange = true;
}
// show only items that are the root and its children
public void FillList(Transform root)
{
foreach (Transform child in root)
{
for (int i = 0; i < MyListOfStuff.Count; ++i)
{
if (MyListOfStuff[i].Name == child.name)
{
MyListOfStuff[i].enable();
MyListOfStuff[i].ToggleChildren = false;
MyListOfStuff[i].GuiStyle = MyListOfStuff[i].UnSelectedStyle;
}
}
}
}
// turn off children objects
public void ClearList(Transform root)
{
//Debug.Log(root.name);
Transform[] childs = root.GetComponentsInChildren<Transform>();
foreach (Transform child in childs)
{
for (int i = 0; i < MyListOfStuff.Count; ++i)
{
if (MyListOfStuff[i].Name == child.name && MyListOfStuff[i].Name != root.name)
{
MyListOfStuff[i].disable();
MyListOfStuff[i].ToggleChildren = false;
MyListOfStuff[i].GuiStyle = MyListOfStuff[i].UnSelectedStyle;
}
}
}
}
// recursively build the list so the hierarchy is in tact
void BuildList(Transform root)
{
// for every object in the thing we are viewing
foreach (Transform child in root)
{
// add the item
MyListOfStuff.Add(new GuiListItem(false, child.name));
// if it has children add the children
if (child.childCount > 0)
{
BuildList(child);
}
}
}
public void ResetDropDownList()
{
selectedItemCaption = "";
ClearList(root);
FillList(root);
}
public string RemoveNumbers(string key)
{
return Regex.Replace(key, @"\d", "");
}
// sets the drop list elements to use the correct GUI skin custom style
private void SetupGUISetting()
{
// set drop down list gui
int depth = 0;
// check all the parts for hierarchy depth
for (int i = 0; i < MyListOfStuff.Count; ++i)
{
GameObject currentObject = GameObject.Find(MyListOfStuff[i].Name);
Transform currentTransform = currentObject.transform;
depth = 0;
if (currentObject.transform.parent == root) // if under root
{
if (currentObject.transform.childCount > 0)
{
MyListOfStuff[i].GuiStyle = depth;
MyListOfStuff[i].UnSelectedStyle = depth;
MyListOfStuff[i].SelectedStyle = depth + 2;
}
else
{
MyListOfStuff[i].GuiStyle = depth + 1;
MyListOfStuff[i].UnSelectedStyle = depth + 1;
MyListOfStuff[i].SelectedStyle = depth + 1;
}
MyListOfStuff[i].Depth = depth;
}
else // if not under root find depth
{
while (currentTransform.parent != root)
{
++depth;
currentTransform = currentTransform.parent;
}
MyListOfStuff[i].Depth = depth;
// set gui basied on depth
if (currentObject.transform.childCount > 0)
{
MyListOfStuff[i].GuiStyle = depth * 3;
MyListOfStuff[i].UnSelectedStyle = depth * 3;
MyListOfStuff[i].SelectedStyle = (depth * 3) + 2;
}
else
{
MyListOfStuff[i].GuiStyle = depth * 3 + 1;
MyListOfStuff[i].UnSelectedStyle = depth * 3 + 1;
MyListOfStuff[i].SelectedStyle = depth * 3 + 1;
}
}
}
}
}
Unity3D常用代码总结的更多相关文章
- Unity3d 常用代码
//创建一个名为"Player"的游戏物体 //并给他添加刚体和立方体碰撞器. player=new GameObject("Player"); player. ...
- (转) Unity3D常用代码收集总结
//创建一个名为"Player"的游戏物体 //并给他添加刚体和立方体碰撞器. player=new GameObject("Player"); player. ...
- Unity3D常用代码集合
1.基本碰撞检测代码 function OnCollisionEnter(theCollision : Collision){ if(theCollision.gameObject.n ...
- GCD 常用代码
GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...
- 转--Android实用的代码片段 常用代码总结
这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下 1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...
- 刀哥多线程之03GCD 常用代码
GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...
- jquery常用代码集锦
1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 ) 1 2 3 4 5 $.ajaxSetup({ ajaxSettings : { contentT ...
- Mysql:常用代码
C/S: Client Server B/S: Brower Server Php主要实现B/S .net IIS Jave TomCat LAMP:L Mysql:常用代码 Create table ...
- javascript常用代码大全
http://caibaojian.com/288.html 原文链接 jquery选中radio //如果之前有选中的,则把选中radio取消掉 $("#tj_cat .pro_ca ...
随机推荐
- Dynamic CRM 2013学习笔记(十)客户端几种查询数据方式比较
我们经常要在客户端进行数据查询,下面分别比较常用的几种查询方式:XMLHttpRequest, SDK.JQuery, SDK.Rest. XMLHttpRequest是最基本的调用方式,JQuery ...
- Dynamic CRM 2013学习笔记(四十一)流程4 - 异步工作流(Workflow)用法图解
在CRM 2013 里,工作流被分成二类:异步工作流和实时工作流.异步工作流依赖一个windows 服务: Microsoft Dynamics CRM Asynchronous Processing ...
- C#分布式缓存二:Asp.Net中使用Couchbase
前言 上一篇<C#分布式缓存一:Couchbase的安装与简单使用>主要讲解对Couchbase服务端的安装配置和客户端的引用调用,通过代码来完成最简单的实现调用.本次通过简单的配置,来完 ...
- 理解 Soap
http://www.cnblogs.com/yhuang/archive/2012/04/04/share_storm.html 自己也写了下: using System; using System ...
- autohotkey在运维中的应用
AutoHotkey是一个自由.开源的宏生成器和自动化软件工具,它让用户能够自动执行重复性任务.AutoHotkey可以修改任何应用程序的用户界面(例如,把默认的Windows按键控制命令替 ...
- AngularJS快速入门指南02:介绍
AngularJS是一个JavaScript框架.它可以通过<script>标记被添加到HTML页面中. AngularJS通过指令对HTML属性进行了扩展,然后通过表达式将数据绑定到HT ...
- 如何带领一个Android开发团队
1)重构,夜未眠 将框架从业务中剥离 如何提高开发效率 如何提高程序性能 单元测试 技术调研 代码版本管理 2)渠道包管理 自动打包工具 批量打渠道包的两种解决方案 定制渠道包的流程管理 3)稳定性, ...
- CLIQUE 聚类算法以及Java实现+多线程
CLIQUE(Clustering In QUEst)是一种简单的基于网格的聚类方法,用于发现子空间中基于密度的簇.CLIQUE把每个维划分成不重叠的区间,从而把数据对象的整个嵌入空间划分成单元.它使 ...
- LBS定位技术
http://www.cnblogs.com/LBSer/p/3295642.html LBS定位技术从方法上可分成三类:基于三角关系的定位技术.基于场景分析的定位技术.基于临近关系的定位技术(唐毅和 ...
- Redis安装与日常使用
下载与安装 $ wget http://download.redis.io/releases/redis-3.0.3.tar.gz $ tar xzf redis-3.0.3.tar.gz $ cd ...