Beispiel #1
0
	IEnumerator SetParametersWhenPlaying(MaterialFx fx)
	{
		float endTime = Time.time + fx.clip.length;	
		while (Time.time <= endTime)
		{
			if (ani.isPlaying)
			{
				foreach (Material mat in mats)
				{
					foreach (string properity in currentFx.propertyNames)
					{
						if (materialParameters.IsColorProperity(properity))
						{
							mat.SetColor(properity, materialParameters.GetColor(properity));
						}
						else
						{
							mat.SetFloat(properity, materialParameters.GetValue(properity));
						}
					}
				}
			}
			yield return null;
		}

		SetMaterialToOris();
	}
Beispiel #2
0
    public override void OnInspectorGUI()
    {
        //base.OnInspectorGUI();
        MaterialFx matFx = target as MaterialFx;

        EditorGUILayout.LabelField(matFx.IsPlaying?"播放中":"已结束");

        EditorGUI.BeginChangeCheck();
        matFx.m_type = (MaterialFx.enType)EditorGUILayout.Popup("类型", (int)matFx.m_type, MaterialFx.TypeName);

        if (matFx.m_type != MaterialFx.enType.add)
        {
            matFx.m_matIndex = EditorGUILayout.IntField("第几个材质", matFx.m_matIndex);
        }

        if (matFx.m_type != MaterialFx.enType.modify)
        {
            matFx.m_mat = (Material)EditorGUILayout.ObjectField("材质", matFx.m_mat, typeof(Material), false);
        }
        else if (matFx.m_mat != null)
        {
            matFx.m_mat = null;
        }

        if (matFx.m_type == MaterialFx.enType.replace)
        {
            matFx.m_useOldTex = EditorGUILayout.Toggle("使用替换的贴图", matFx.m_useOldTex);
        }

        matFx.m_duration = EditorGUILayout.FloatField("结束时间", matFx.m_duration);

        MaterialAni removeMatAni = null;

        for (int i = 0; i < matFx.m_anis.Count; ++i)
        {
            if (DrawAni(matFx.m_anis[i], i))
            {
                removeMatAni = matFx.m_anis[i];
            }
        }
        if (removeMatAni != null)
        {
            matFx.m_anis.Remove(removeMatAni);
        }

        int idx = UnityEditor.EditorGUILayout.Popup("添加渐变", -1, MaterialAni.TypeName);

        if (idx != -1)
        {
            MaterialAni matAni = new MaterialAni();
            matAni.m_type = (MaterialAni.enType)idx;
            matFx.m_anis.Add(matAni);
        }

        if (EditorGUI.EndChangeCheck())
        {
            //Debuger.Log("修改");
            EditorUtil.SetDirty(matFx);
        }
    }
Beispiel #3
0
 static void AddMaterialFx()
 {
     if (UnityEditor.Selection.activeGameObject != null)
     {
         MaterialFx m = UnityEditor.Selection.activeGameObject.AddComponentIfNoExist <MaterialFx>();
         EditorUtil.SetDirty(m);
     }
 }
Beispiel #4
0
	public void Play(MaterialFx fx)
	{
		StopAllCoroutines();	
		SetMaterialToOris();

		currentFx = fx;

		ani.AddClip(fx.clip, fx.clip.name);
		ani.Play(fx.clip.name);

		StartCoroutine(SetParametersWhenPlaying(fx));
	}
Beispiel #5
0
 static void AddScrollingUV()
 {
     if (UnityEditor.Selection.activeGameObject != null)
     {
         MaterialFx m = UnityEditor.Selection.activeGameObject.AddComponentIfNoExist <MaterialFx>();
         m.m_type = enType.modify;
         MaterialAni ma = new MaterialAni();
         ma.m_type = MaterialAni.enType.ScrollingUV;
         m.m_anis.Add(ma);
         EditorUtil.SetDirty(m);
     }
 }
Beispiel #6
0
	IEnumerator PlayMatFxWithDelay(MaterialFx fx, float delay)
	{
		yield return new WaitForSeconds(delay);
		matFxPlayer.Play(fx);

	}
Beispiel #7
0
	void PlayMatFx(MaterialFx fx, float delay)
	{
		StartCoroutine(PlayMatFxWithDelay(fx, delay));
	}
Beispiel #8
0
 public MaterialHandle(MaterialFx fx, Material mat, MaterialMgr mgr)
 {
     this.fx  = fx;
     this.mat = mat;
     this.mgr = mgr;
 }
Beispiel #9
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);
    }