换装相关

 

texture & texture2D

Texture2D ttt = render.material.GetTexture("_MainTex") as Texture2D;



Use debug console and do the following

Debug.Log(renderer.material.mainTexture);
Debug.Log(renderer.material.mainTexture.GetType()) ;

That will tell us if the mainTexture is null or not and what type of derived object mainTexture is.


_MainTex ("Base (RGB)", 2D) = "white" {}









 
Unity3D IPhone开发之模型换装(换贴图)系统


 

查看unity3d的换装demo,感觉比较繁琐,经自行试验,创建如下换装操作,具体步骤如下:


    1、定义需要换装的GameObject,暂定为:Player

    2、默认贴图贴好。

    3、创建脚本 Cloth.js

   
    var Cloth1 : Texture[];//上衣

    var Cloth2 : Texture[];//裤子
   
    var Properties_style : GUIStyle;//标签样式

    var int1 : int;

    var int2 : int;

    function Start()

    {

        int1 = 0;

        int2 = 0;

    }

    funciton OnGUI()

    {
        GUI.Button(Rect(20, 10, 70,20),"Cloth",Properties_style);
        GUI.Button(Rect(20, 35 + 25, 70,20),"Trousers",Properties_style);
        if (GUI.Button(Rect(100, 10, 20, 20),">"))//换上衣

        {

            int1++;

            if (int1 > Cloth1.length - 1)  int1 = 0;

            renderer.materials[0].mainTexture = Cloth1[int1];//此处materials[0]表示你衣服贴图材质球所处的位置,下面同此

        }

        if (GUI.Button(Rect(100, 35, 20, 20),">"))//换裤子

        {

            int2++;

            if (int2 > Cloth2.length - 1) int2 = 0;

            renderer.materials[1].mainTexture = Cloth2[int2];

        }

    }
 


    4、把Cloth.js绑定到Player上,然后定义Cloth1和Cloth2这2个数组长度,放上相应的Texture,即可运行查看效果了。


由www.J2meGame.com精心收集。

















在换装前,我先讲讲WWW的相关事宜。

    c#里用WWW去读取资源时,需要用到迭代器IEnumerator和 MonoBehaviour的StartCoroutine方法。
    这是在MONO下开线程来读取资源的一种方法。
    在读取资源前,我们必须先建立资源。这种资源建立的方法,需要用到Unity3d里EDIT的API,对指定资源进行打包处理。
    if (BuildPipeline.BuildAssetBundle(o, null, pathd, BuildAssetBundleOptions.CollectDependencies))
    {
    Debug.Log("create assetBundle [" + pathd + "] BINGO!");
    } else {
    Debug.Log("create assetBundle [" + pathd + "] ERROR!!!");
    }

    最后,懂得以上启蒙知识后就可以对人物模型进行换装了。
    对一个人物模型里的个别模块换装,可分为:
    材质更换,
    模型更换,
    和骨架更换。
    就像下面这段程序那样。
    在得到人物SkinnedMeshRenderer后,
    对它的模型进行更换,
    对其骨架进行更换,
    最后对其材质球进行更换。
    如此而已。
    此文虽不详尽,但可以帮住你理解在研究换装过程中的种种问题。我的时间不多,写此文,只为帮助大家从本质上理解人物换装原理。

    SkinnedMeshRenderer r = root.GetComponent<SkinnedMeshRenderer>();
    r.sharedMesh = new Mesh();
    r.sharedMesh.CombineMeshes(combineInstances.ToArray(), false, false);
    r.bones = bones.ToArray();
    r.materials = materials.ToArray();

posted @ 2012-03-13 14:36  渡蓝  阅读(342)  评论(0)    收藏  举报