上次 我们告诉编辑器制作,如何将图像加载到现场,如今 我们要告诉下。怎么样 制造UIButton以及UIimage交换。
阿土。
进入专题。
首先,我们要明白 unity机制。button属性等。
首先 我们先说下 unity的机制:
unity中检測点击事件,使用NGUI的可能知道,NGUI使用的就是SendMessage的方式来进行事件的传递。
没错,这也是 unity最为简便的方式。
(要注意一个问题哦,这个方式 假设 你要使用 大于 万次循环的话 会有延迟的哦,一般也不会同一时候发送万条事件的)。
我们知道了 事件的传送的方式,那么我们就開始制作这个规则。首先 要使用SendMessage的话我们要拿到gameobject的对象才干使用。
所以我们要想办法拿到点击的那个物体。怎么坐那,原理事实上非常easy,就是射线。
我们都知道,unity的射线是用来探測使用的 正好。我们全部能够看到的物体都是存在camera以下,那么我们就能够使用,从camera上发射射线,来达到检測物体的作用,物体要有碰撞体哦。
先贴部分代码等这个章节完了我会贴完整的代码
Ray cameraRay; RaycastHit hit; Vector3 touchPos,pressOffSet; public static GameObject touchObj = null; public static VKCamera shareVkCamera;
void Update () { #if UNITY_EDITOR if(Input.GetMouseButtonDown(0)){ onPressDown(Input.mousePosition); }else if(Input.GetMouseButtonUp(0)){ onPressUp(Input.mousePosition); }else if(Input.GetMouseButton(0)){ onDrag(Input.mousePosition); } #endif #if UNITY_IPHONE || UNITY_ANDROID Touch touch; if(Input.touchCount==1){ touch = Input.GetTouch (0); switch(touch.phase){ case TouchPhase.Began: onPressDown(touch.position); break; case TouchPhase.Moved: onPressUp(touch.position); break; case TouchPhase.Ended: case TouchPhase.Canceled: onDrag(touch.position); break; } } #else if(Input.GetMouseButtonDown(0)){ onPressDown(Input.mousePosition); }else if(Input.GetMouseButtonUp(0)){ onPressUp(Input.mousePosition); }else if(Input.GetMouseButton(0)){ onDrag(Input.mousePosition); } #endif }
public void onPressDown(Vector2 vec){ touchPos = vec; for(int i=0 ; i()){ touchPos = Camera.allCameras[i].ScreenToWorldPoint(touchPos); pressOffSet =touchObj.transform.position-touchPos; VKButton button = touchObj.GetComponent (); if(!iSNull(button.pressEventName) && button.eventObj!=null) button.eventObj.SendMessage(button.pressEventName,button); if(button.pressButtonTex!=null){ button.renderer.sharedMaterial.mainTexture = button.pressButtonTex; } if(button.isDrag && !iSNull(button.dragStartEventName)){ button.eventObj.SendMessage(button.dragStartEventName,vec); } if(button.isAni){ button.SendMessage("onPressAni"); } } } } }
public void onPressUp(Vector2 vec){ if(touchObj!=null){ VKButton button = touchObj.GetComponent(); if(button!=null){ if(button.buttonTex!=null){ touchObj.renderer.sharedMaterial.mainTexture = touchObj.GetComponent ().buttonTex; } if(!iSNull(button.clickEventName) && button.eventObj!=null){ button.eventObj.SendMessage(button.clickEventName,button); } if(button.isDrag && !iSNull(button.dragEndEventName)){ button.SendMessage(button.dragEndEventName,vec); } if(button.isAni){ button.SendMessage("onClickAni"); } } touchObj = null; } }
public void onDrag(Vector2 vec){ if(touchObj!=null){ VKButton button = touchObj.GetComponent(); if(button!=null && button.isDrag){ for(int i= 0;i
bool iSNull(string eventName){ bool buttonIsNull = false; if(eventName== null || eventName.Equals("null")){ buttonIsNull =true; } return buttonIsNull; }
这些是camera的部分,另一点这个 我们没有贴我的适配方案 摄像机的适配方案。这里统配是1420*800的设计方案。由于这里我预计还要改下,发现这个摄像机适配方案,超出了unity的物理引擎的范围。不建议。只是能够參考。
public void initVKCamere(){ gameObject.name = "VKCamere"; this.camera.orthographic = true; this.camera.backgroundColor = Color.black; this.camera.nearClipPlane = 0; this.camera.farClipPlane = 9999; this.camera.orthographic =true; this.camera.orthographicSize = getCameraSize (); this.transform.position = new Vector3(0,0,-1000); this.transform.rotation = Quaternion.Euler(Vector3.zero); this.transform.localScale = Vector3.one; Application.targetFrameRate=60; if(GetComponent()){ //DestroyImmediate(GetComponent ()); } } int getCameraSize(){ int size = 384; bool isLandscape=(Camera.main.pixelWidth>Camera.main.pixelHeight); float rad = Camera.main.pixelWidth/Camera.main.pixelHeight; bool isIPad= (Mathf.Abs(rad-1.3333f)<0.001f) || (Mathf.Abs(rad-0.75f)<0.001f); if(isIPad){ if(isLandscape){ size=400; }else{ size=533; } }else{ if(isLandscape){ size=400; }else{ //iPhone 5 if(Camera.main.pixelHeight/Camera.main.pixelWidth>1.6){ size=710; }else{ size=600; } } } return size; }
camera的编辑器类
using UnityEditor;using UnityEngine;using System.Collections;[CustomEditor(typeof(VKCamera))]public class VKCameraEditor : Editor { public override void OnInspectorGUI (){ base.OnInspectorGUI(); VKCamera vkCamere = (VKCamera)target; if(GUILayout.Button("ReSetCamera")){ vkCamere.initVKCamere(); } EditorUtility.SetDirty(vkCamere); EditorUtility.UnloadUnusedAssets(); }}好以下我们来讲述下,button的一些东西,事实上就非常easy了。
仅仅是在编辑器方面可能会有些麻烦。
说白了 就是button就来处理 摄像机发来的东西。
尽管非常easy的一句话。但我们写得东西还是有非常多的。
依旧,button还是继承前面的view
using UnityEngine;using System.Collections;using Holoville.HOTween;//[RequireComponent(typeof(BoxCollider))]public class VKButton : VKView { [HideInInspector] public Material buttonDefultMat; [HideInInspector] public Mesh buttonDefultMesh; [HideInInspector] public Texture buttonTex,pressButtonTex; [HideInInspector] public string pressEventName = null; [HideInInspector] public string clickEventName = null; [HideInInspector] public string dragStartEventName = null; [HideInInspector] public string dragEventName = null; [HideInInspector] public string dragEndEventName = null; [HideInInspector] public GameObject eventObj = null; [HideInInspector] public string eventScript = null; [HideInInspector] public bool isDrag = false; [HideInInspector] public float scale = 1; [HideInInspector] public string info= null; [HideInInspector] public string[] animatorType = new string[]{"null","bigger","smaller","moveLeft","moveRight"}; [HideInInspector] public int currentAnimator = 0; [HideInInspector] public bool isAni = false; void Start () { buttonDefultMat = new Material(Shader.Find("VK/VKButtonShader")); gameObject.GetComponent这里有个 动画效果 我是自己使用的hotween整合的一些东西,假设你想用自己的能够自己去做。hotween插件 我就不用发布了,能够去官网进行下载,各大论坛也有下载资源。().material = buttonDefultMat; buttonDefultMesh = new Mesh (); gameObject.GetComponent ().mesh = buttonDefultMesh; if(GetComponent ()== null) gameObject.AddComponent (); updateButton(); } public float setScale{ set{ scale = value; }get{ return scale; } } public void updateButton(){ if(buttonTex!=null){ if(buttonDefultMat!=null) buttonDefultMat.mainTexture = buttonTex; if(buttonDefultMesh!=null) buttonDefultMesh.vertices = InitBase.initVertice(buttonTex.width *scale,buttonTex.height*scale,ancPointx,ancPointy); if(this!=null&&gameObject.GetComponent ()!=null){ gameObject.GetComponent ().size = new Vector3(buttonTex.width*scale,buttonTex.height*scale,0); } }else{ if(buttonDefultMat!=null) buttonDefultMat.mainTexture = null; if(buttonDefultMesh!=null) buttonDefultMesh.vertices = InitBase.initVertice(width*scale,height*scale,ancPointx,ancPointy); gameObject.GetComponent ().size = new Vector3(width,height,0); } if(buttonDefultMesh!= null){ buttonDefultMesh.triangles = InitBase.initTri (); buttonDefultMesh.normals = InitBase.initNormal(); buttonDefultMesh.uv = InitBase.initUV(); } } Vector3 pressScale= Vector3.zero; Vector3 pressPos = Vector3.zero; Tweener tw = null; void onPressAni(){ if(tw!=null && !tw.isComplete){ return; } pressScale = transform.localScale; pressPos = transform.position; switch(currentAnimator){ case 1: tw = VKAnimator.scaleBy(gameObject,0.3f,Vector3.one*0.2f,VkAniDuration.AnimationCurve,null); break; case 2: tw = VKAnimator.scaleBy(gameObject,0.3f,Vector3.one*-0.2f,VkAniDuration.AnimationCurve,null); break; case 3: tw = VKAnimator.moveBy(gameObject,0.3f,new Vector3(-10f,0,0),false,VkAniDuration.AnimationCurve,null); break; case 4: tw = VKAnimator.moveBy(gameObject,0.3f,new Vector3(10f,0,0),false,VkAniDuration.AnimationCurve,null); break; } } void onClickAni(){ if(currentAnimator == 1 || currentAnimator==2){ VKAnimator.scaleTo(gameObject,0.3f,pressScale,VkAniDuration.AnimationCurve,null); } else if(currentAnimator == 3 || currentAnimator==4){ VKAnimator.moveTo(gameObject,0.3f,pressPos,false,VkAniDuration.AnimationCurve,null); } } public void switchImageView(){ VKImageView imgView = gameObject.AddComponent (); imgView.imgViewTex = buttonTex; imgView.highLightedTex = pressButtonTex; imgView.ancPointx = ancPointx; imgView.ancPointy = ancPointy; imgView.scale = scale; imgView.updateImageView (); if(GetComponent ()){ DestroyImmediate(GetComponent ()); } DestroyImmediate (GetComponent ()); }}
using UnityEngine;using System.Collections;using Holoville.HOTween;public enum VkAniDuration{ AnimationCurve = EaseType.AnimationCurve, EaseInBack=EaseType.EaseInBack, EaseInBounce=EaseType.EaseInBounce, EaseInCirc=EaseType.EaseInCirc, EaseInCubic=EaseType.EaseInCubic, EaseInElastic=EaseType.EaseInElastic, EaseInExpo=EaseType.EaseInExpo, EaseInOutBack=EaseType.EaseInOutBack, EaseInOutBounce=EaseType.EaseInOutBounce, EaseInOutCirc=EaseType.EaseInOutCirc, EaseInOutCubic=EaseType.EaseInOutCubic, EaseInOutElastic=EaseType.EaseInOutElastic, EaseInOutExpo=EaseType.EaseInOutExpo, EaseInOutQuad=EaseType.EaseInOutQuad, EaseInOutQuart=EaseType.EaseInOutQuart, EaseInOutQuint=EaseType.EaseInOutQuint, EaseInOutSine=EaseType.EaseInOutSine, EaseInQuad=EaseType.EaseInQuad, EaseInQuart=EaseType.EaseInQuart, EaseInQuint=EaseType.EaseInQuint, EaseInSine=EaseType.EaseInSine, EaseOutBack=EaseType.EaseOutBack, EaseOutBounce=EaseType.EaseOutBounce, EaseOutCirc=EaseType.EaseOutCirc, EaseOutCubic=EaseType.EaseOutCubic, EaseOutElastic=EaseType.EaseOutElastic, EaseOutExpo=EaseType.EaseOutExpo, EaseOutQuad=EaseType.EaseOutQuad, EaseOutQuart=EaseType.EaseOutQuart, EaseOutQuint=EaseType.EaseOutQuint, EaseOutSine=EaseType.EaseOutSine, Linear=EaseType.Linear};public class VKAnimator {// public static Tweener moveTo(GameObject obj,float time,Vector3 target,bool isLocal,VkAniDuration vkDurType = VkAniDuration.AnimationCurve){// Tweener tw = null;// if(isLocal)// tw = startVkAnimote(obj,time,"localPosition",target,vkDurType,null);// // else// tw = startVkAnimote(obj,time,"position",target,vkDurType,null);// return tw;// } public static Tweener moveTo(GameObject obj,float time,Vector3 target,bool isLocal,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){ Tweener tw = null; if(isLocal) tw =startVkAnimote(obj,time,"localPosition",target,vkDurType,completeObj); else tw = startVkAnimote(obj,time,"position",target,vkDurType,completeObj); return tw; } public static Tweener moveBy(GameObject obj,float time,Vector3 offset,bool isLocal,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){ Tweener tw = null; if(isLocal){ Vector3 tLocalPos = obj.transform.localPosition + offset; tw = startVkAnimote(obj,time,"localPosition",tLocalPos,vkDurType,completeObj); }else{ Vector3 tPos = obj.transform.position + offset; tw = startVkAnimote(obj,time,"position",tPos,vkDurType,completeObj); } return tw; } public static Tweener scaleTo(GameObject obj,float time,Vector3 scale,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){ Tweener tw = null; tw = startVkAnimote(obj,time,"localScale",scale,vkDurType,completeObj); return tw; } public static Tweener scaleBy(GameObject obj,float time,Vector3 offsetScale,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){ Tweener tw = null; Vector3 targetScale = obj.transform.localScale+offsetScale; tw = startVkAnimote(obj,time,"localScale",targetScale,vkDurType,completeObj); return tw; } public static Tweener routeTo(GameObject obj,float time,Vector3 routeEulerTarget,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){ Tweener tw = null; tw = startVkAnimote(obj,time,"localEulerAngles",routeEulerTarget,vkDurType,completeObj); return tw; } public static Tweener routeBy(GameObject obj,float time,Vector3 routeEulerOffset,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){ Tweener tw = null; Vector3 target = obj.transform.localEulerAngles+ routeEulerOffset; tw = startVkAnimote(obj,time,"localEulerAngles",target,vkDurType,completeObj); return tw; } //localEulerAngles private static Tweener startVkAnimote(GameObject obj,float time,string key,object target,VkAniDuration vkDurType = VkAniDuration.AnimationCurve,VKAniComplete completeObj = null){ Tweener tw = null; TweenParms twp = new TweenParms(); twp.Prop(key,target); twp.Ease((EaseType)vkDurType); if(completeObj!=null){ twp.OnComplete(completeObj.sendTargetObj,completeObj.methodName,completeObj.sendobj,SendMessageOptions.DontRequireReceiver); } tw = HOTween.To(obj.transform,time,twp); return tw; }}
using UnityEngine;using System.Collections;public class VKAniComplete{ public GameObject sendTargetObj = null; public string methodName = null; public object sendobj = null; public VKAniComplete(GameObject sendTargetObj,string methodName,object sendobj){ this.sendTargetObj = sendTargetObj; this.methodName = methodName; this.sendobj = sendobj; }}以下就是button的编辑器方面了。
ok,这里可能有点麻烦。
只是还是能够解决的。
首先我们要获取脚本里面的方法怎么获取那。依据c#的MSDN的描写叙述我们能够看到。就是下面方法。这就是 为什么 NGUI 仅仅能找到public属性的方法。
using UnityEngine;using System.Collections;using System.Reflection;public class GetPublic : MonoBehaviour { static public MethodInfo[] GetObjectMethods(string selectedObjClass){ if(selectedObjClass==null)return null; MethodInfo[] methodInfos=null; if(System.Type.GetType(selectedObjClass)==null){ return methodInfos; } methodInfos = System.Type.GetType(selectedObjClass).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); return methodInfos; }}这里就是button编辑器的一些属性的传递,和界面的改动哈。
using UnityEngine;using System.Collections;using UnityEditor;using System.Reflection;[CustomEditor(typeof(VKButton))]public class VKButtonEditor : Editor { public override void OnInspectorGUI () { int pressEventNum= 0; int selectScriptNum = 0; int clickEventNum = 0; int dragStartEveNum = 0; int dragEveNum = 0; int dragEndEveNum = 0; VKButton button = (VKButton)target; button.buttonTex = EditorGUILayout.ObjectField ("Texture",button.buttonTex,typeof(Texture),true)as Texture; button.pressButtonTex = EditorGUILayout.ObjectField ("PressButtonTex",button.pressButtonTex,typeof(Texture),true)as Texture; button.eventObj = EditorGUILayout.ObjectField("eventObj",button.eventObj,typeof(GameObject),true) as GameObject; button.info = EditorGUILayout.TextField("info",button.info); button.ancPointx = EditorGUILayout.Slider("AnchorX",button.ancPointx,0.0f,1.0f); button.ancPointy = EditorGUILayout.Slider("AnchorY",button.ancPointy,0.0f,1.0f); if(button.buttonTex == null){ button.width=EditorGUILayout.IntField("Width",button.width); button.height=EditorGUILayout.IntField("Height",button.height); } GUILayout.BeginHorizontal(); if(GUILayout.Button("2X")){ button.setScale = 0.5f; } if(GUILayout.Button("1X")){ button.setScale = 1f; } if(GUILayout.Button("1.5X")){ button.setScale = 0.75f; } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if(GUILayout.Button("ChangeName")){ if(button.buttonTex!=null){ button.name = button.buttonTex.name; } } if(GUILayout.Button("Switch ImageView")){ button.switchImageView(); } GUILayout.EndHorizontal(); if(button.eventObj!=null){ MonoBehaviour[] monoScriptArry =new MonoBehaviour[button.eventObj.GetComponents我在这里扩展一些功能。像移动啊。什么。().Length+1];// button.eventObj.GetComponents (); for(int i=0;i ()[i]; if(i == monoScriptArry.Length-1){ monoScriptArry [i] = null; } } if(monoScriptArry !=null&& monoScriptArry.Length>0){ string [] scriptName = new string[monoScriptArry.Length]; for(int i=0;i 0){ string[] methodArry = new string[publicMethodArry.Length]; for(int i=0;i
。其实还可以添加一些功能 。这里只是学习。
有兴趣的东西可以添加你自己。