首页 > 代码库 > unity编辑器和插件(三)

unity编辑器和插件(三)

 接下来 我就开始正规的编辑器和插件的制作。


我这里是按照 IOS android的 控件为例子模板。来制作 一套使用 unity书写的控件。


好首先我们来想下  IOS 和android的 控件都是以四边形基础的。

SO,

接下来 我们来转像unity,在unity中 我们怎么去建立一个四边形那, 依照图形绘制的原理。三点一面的理论。我们可以使用6个点来建立一个两三角形组成的四边形。

我们先学会怎么去绘制一个view,也就是简单的一个色面。

我们知道 unity中得面也是采取这种的绘制方法,绘制一个面就会多一个drawcall,为了面绘制的节省,我们就会使用面合并的原理,来减少drawcall的产生(opengl自动处理)。

好,我们来定义六个点:

	public static int[] initTri(){
		int[] triangle = new int[6];
		triangle [0] = 0;
		triangle [1] = 2;
		triangle [2] = 1;
		triangle [3] = 2;
		triangle [4] = 3;
		triangle [5] = 1;
		return triangle;
	}
有了 6个点,我们就要去定义三角形的大小。

public static Vector3 [] initVertice(float width ,float height,float ancPointx,float ancPointY){
        Vector3 [] viewVer = new Vector3[4];
        viewVer [0] = new Vector3 (-ancPointx*-width,-ancPointY*height,0);
        viewVer [1] = new Vector3 ((1-ancPointx)*-width,-ancPointY*height,0);
        viewVer [2] = new Vector3 (-ancPointx*-width,(1-ancPointY)*height,0);
        viewVer [3] = new Vector3 ((1-ancPointx)*-width,(1-ancPointY)*height,0);
        return viewVer;
    }
其中ancPointxancPointy,为 我们经常用到的锚点,widthheight就是你要绘制的面的宽和高了。

好了 有了这些东西 现在制作一个没有任何颜色的几条线组成的线框。


我们为了让这个面有颜色,必学要有 法线 和 材质,shader 这样才是一个完整的 色面。

法线

public static Vector3[] initNormal(){
        Vector3[] normals = new Vector3[4];
        normals[0] = -Vector3.forward;
        normals[1] = -Vector3.forward;
        normals[2] = -Vector3.forward;
        normals[3] = -Vector3.forward;
        return normals;
    }

当然如果还想去绘制图片在面上就要有 UV的的定义

public static Vector2 [] initUV(){
        Vector2 [] uv = new Vector2[4];
        uv[0] = new Vector2(0, 0);
        uv[1] = new Vector2(1, 0);
        uv[2] = new Vector2(0, 1);
        uv[3] = new Vector2(1, 1);
        return uv;
    }

想好这些东西,我们就开始定义面了。


源码奉上!


公用类!

using UnityEngine;
using System.Collections;

public class InitBase : MonoBehaviour {

    public static Vector3 [] initVertice(float width ,float height,float ancPointx,float ancPointY){
        Vector3 [] viewVer = new Vector3[4];
        viewVer [0] = new Vector3 (-ancPointx*-width,-ancPointY*height,0);
        viewVer [1] = new Vector3 ((1-ancPointx)*-width,-ancPointY*height,0);
        viewVer [2] = new Vector3 (-ancPointx*-width,(1-ancPointY)*height,0);
        viewVer [3] = new Vector3 ((1-ancPointx)*-width,(1-ancPointY)*height,0);
        return viewVer;
    }
public static int[] initTri(){
        int[] triangle = new int[6];
        triangle [0] = 0;
        triangle [1] = 2;
        triangle [2] = 1;
        triangle [3] = 2;
        triangle [4] = 3;
        triangle [5] = 1;
        return triangle;
    }
    
    public static Vector3[] initNormal(){
        Vector3[] normals = new Vector3[4];
        normals[0] = -Vector3.forward;
        normals[1] = -Vector3.forward;
        normals[2] = -Vector3.forward;
        normals[3] = -Vector3.forward;
        return normals;
    }
    
    public static Vector2 [] initUV(){
        Vector2 [] uv = new Vector2[4];
        uv[0] = new Vector2(0, 0);
        uv[1] = new Vector2(1, 0);
        uv[2] = new Vector2(0, 1);
        uv[3] = new Vector2(1, 1);
        return uv;
    }

}

物体上的类View

using UnityEngine;
using System.Collections;


[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
[ExecuteInEditMode]
public class VKView : MonoBehaviour {
	
	Mesh viewMesh;
	Material viewDefultMat;
	bool isChange;
	[HideInInspector] public float ancPointx=0.5f,ancPointy = 0.5f;
	[HideInInspector] public int width = 100,height = 100;
	public string test;
	// Use this for initialization
	void Start () {
		viewDefultMat = new Material (Shader.Find ("VK/VKViewShader"));
		gameObject.GetComponent<MeshRenderer> ().sharedMaterial = viewDefultMat;
		viewMesh = new Mesh ();
		gameObject.GetComponent<MeshFilter> ().mesh = viewMesh;
		updateView();
	}
	
	public void updateView(){
		if (viewDefultMat != null) {
			viewMesh.vertices = InitBase.initVertice(width,height,ancPointx,ancPointy);
			viewMesh.triangles = InitBase.initTri();
			viewMesh.normals = InitBase.initNormal();
			viewMesh.uv = InitBase.initUV();
		}
	}
#if UNITY_EDITOR
	void OnDrawGizmosSelected(){
		Gizmos.color = Color.blue;
		Gizmos.DrawWireCube (transform.position,new Vector3(width,height,0f));
	}
#endif
}

view的编辑器类

using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(VKView))]
[ExecuteInEditMode]
public class VKViewEditor : Editor {

	public override void OnInspectorGUI ()
	{
		base.OnInspectorGUI ();
		VKView vkView = (VKView)target;
//		vkView.tex = EditorGUILayout.ObjectField ("Texture",vkView.tex,typeof(Texture),true)as Texture;
		vkView.width=EditorGUILayout.IntField("Width",vkView.width);
		vkView.height=EditorGUILayout.IntField("Height",vkView.height);
		vkView.test = EditorGUILayout.TextField("面板显示的名字:",vkView.test);

		vkView.updateView ();
		EditorUtility.SetDirty(vkView);
		EditorUtility.UnloadUnusedAssets();
	}
	
}

还有shader

Shader "VK/VKViewShader" {
	Properties {
        _Color ("Main Color", Color) = (1,1,1,1)
    }
  
    SubShader {
        Pass {
       	 Cull Front
       	 Color [_Color]
        }
    }
}



所有代码奉出,学习使用,不会的可以留言哦