Пример #1
0
        public Material GetMaterial(MaterialHandle handle)
        {
            int handleID = (int)handle;

            if (handleID >= m_Materials.Length)
            {
                Debug.LogError(string.Format("Material {0} is not registered.",
                                             Enum.GetName(typeof(MaterialHandle), handleID)));
                return(null);
            }

            return(m_Materials[handleID]);
        }
Пример #2
0
    public void Remove(MaterialHandle h)
    {
        if (this == null)
        {
            return;                 //可能已经被销毁
        }
        if (h.m_shareThing == null) //防止死锁
        {
            return;
        }

        Cache();

        int idx = m_materials.IndexOf(h.m_shareThing);

        if (idx == -1)
        {
            Debuger.LogError("逻辑错误,找不到shareThing");
            h.m_shareThing = null;//防止死锁
            return;
        }


        //从shareThing中删除
        ShareThing st         = m_materials[idx];
        bool       needChange = st.Get() == h; //如果最顶的不是h,那么肯定是不用改变render的材质的

        st.Remove(h);                          //remove里执行了h.m_shareThing = null,必须保证在h.fx.Stop()之前执行,不然可能造成死锁

        //Remove和stop可能会互相调用,要判断下内部已经做了判断
        h.fx.Stop();

        //不需要改材质
        if (!needChange)
        {
            return;
        }

        //计算要改的材质
        bool           remove = false;
        Material       newMat = null;
        MaterialHandle newH   = st.Get <MaterialHandle>();

        if (newH == null)//已经没有特效材质的情况,这个时候有原始材质就换回原始材质,没有就删除
        {
            if ((bool)st.m_param2)
            {
                newMat = (Material)st.m_param;
            }
            else
            {
                m_materials.RemoveAt(idx);
                remove = true;
            }
        }
        else//有别的特效材质的情况
        {
            newMat = newH.mat;
        }

        //真正把材质放到Render上
        Material[] mats   = m_render.sharedMaterials;
        int        length = remove ? mats.Length - 1 : mats.Length;

        Material[] newMats = new Material[length];
        int        curIdx  = 0;

        for (int i = 0; i < mats.Length; ++i)
        {
            if (i == idx)
            {
                if (remove == false)
                {
                    newMats[curIdx] = newMat;
                }
            }
            else
            {
                newMats[curIdx] = mats[i];
            }

            if (i != idx || remove == false)
            {
                ++curIdx;
            }
        }
        m_render.sharedMaterials = newMats;
    }
Пример #3
0
    public MaterialHandle Add(MaterialFx fx)
    {
        Cache();

        Material startMat = null;

        //获取原始材质
        if (fx.m_type == MaterialFx.enType.replace || fx.m_type == MaterialFx.enType.modify)
        {
            if (fx.m_matIndex >= m_materials.Count || m_materials[fx.m_matIndex].m_param == null)
            {
                Debuger.LogError("找不到对应材质,或者要改变材质的render材质丢失 fx:{0} index:{1}", fx.name, fx.m_matIndex);
                return(null);
            }
            startMat = (Material)m_materials[fx.m_matIndex].m_param;
        }


        //创建新材质
        Material newMat;

        if (fx.m_type == MaterialFx.enType.add || fx.m_type == MaterialFx.enType.replace)
        {
            if (fx.m_mat == null)
            {
                Debuger.LogError("材质没有填 fx:{0} ", fx.name);
                return(null);
            }
            if (fx.m_anis.Count == 0 && (fx.m_type == MaterialFx.enType.add || !fx.m_useOldTex))//不需要渐变的情况下材质不需要复制
            {
                newMat = fx.m_mat;
            }
            else
            {
                newMat      = new Material(fx.m_mat);
                newMat.name = fx.m_mat.name + "(Clone)";
            }
        }
        else if (fx.m_type == MaterialFx.enType.modify)
        {
            newMat      = new Material(startMat);
            newMat.name = startMat.name + "(Clone)";
        }
        else
        {
            Debuger.LogError("未知的类型:{0}", fx.m_type);
            return(null);
        }

        //如果有多个(Clone)后缀,那只保留一下
        //if (newMat.name.EndsWith("(Clone)(Clone)"))
        //newMat.name = newMat.name.Replace("(Clone)", "") + "(Clone)";

        //替换新材质的贴图
        if (fx.m_useOldTex && startMat != null)
        {
            Texture tex;
            if (startMat.HasProperty("_MainTex"))
            {
                tex = startMat.GetTexture("_MainTex");
            }
            else if (startMat.HasProperty("_MainTexAlpha"))
            {
                tex = startMat.GetTexture("_MainTexAlpha");
            }
            else
            {
                Debuger.LogError("找不到老材质的贴图 fx:{0} index:{1}", fx.name, fx.m_matIndex);
                return(null);
            }

            if (newMat.HasProperty("_MainTex"))
            {
                newMat.SetTexture("_MainTex", tex);
            }
            else if (newMat.HasProperty("_MainTexAlpha"))
            {
                newMat.SetTexture("_MainTexAlpha", tex);
            }
            else
            {
                Debuger.LogError("找不到新材质的贴图 fx:{0} index:{1}", fx.name, fx.m_matIndex);
                return(null);
            }
        }

        //添加到共享事物里进行管理
        MaterialHandle handle = new MaterialHandle(fx, newMat, this);
        ShareThing     st;
        int            idx;

        if (fx.m_type == MaterialFx.enType.add)
        {
            st          = new ShareThing();
            st.m_param  = null;  //原始材质记录下来,在这里没有原始材质
            st.m_param2 = false; //表明不是是角色创建的时候就有的材质
            m_materials.Add(st);
            st.Add(handle);
            idx = m_materials.Count - 1;
        }
        else
        {
            st = m_materials[fx.m_matIndex];
            st.Add(handle);
            idx = fx.m_matIndex;
        }


        //真正把材质放到Render上
        bool needChange = handle == st.Get();

        if (needChange)
        {
            Material[] mats    = m_render.sharedMaterials;
            int        length  = Mathf.Max(idx + 1, mats.Length);
            Material[] newMats = new Material[length];
            for (int i = 0; i < length; ++i)
            {
                if (i == idx)
                {
                    newMats[i] = newMat;
                }
                else
                {
                    newMats[i] = mats[i];
                }
            }
            m_render.sharedMaterials = newMats;
        }

        return(handle);
    }