為了在unity 中制作奶茶混合效果闯团,使用流體插件 Water2dPro
這里記錄使用過程中遇到的問題
插件固定了生成流體類型的方法,無法自定義更改流體材質(zhì)及預制
解決方案:修改 Water2D_Spawner
的Inspector 腳本,自定義顯示
//Water2DEditor.cs 684行左右
SerializedProperty waterM = serializedObject.FindProperty("WaterMaterial");
SerializedProperty waterObj = serializedObject.FindProperty("DropObject");
EditorGUILayout.PropertyField(waterM);
EditorGUILayout.PropertyField(waterObj);
插件自動生成的攝像機組件包含背景
解決方案: 注釋背景攝像機代碼
ResizeQuadEffectController.cs
// if (gameObject.transform.parent.parent.Find("0-BGCamera"))
// backgroundCamera = gameObject.transform.parent.parent.Find("0-BGCamera").gameObject;
//
// // Only for toon style
// if (backgroundCamera != null)
// {
// RenderTexture BackgroundRT = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
// GetComponent<MeshRenderer>().sharedMaterial.SetTexture("_BackgroundTex", BackgroundRT);
// backgroundCamera.GetComponent<Camera>().forceIntoRenderTexture = true;
// backgroundCamera.GetComponent<Camera>().targetTexture = BackgroundRT;
// }
這里要注意以清,該腳本會獲取主攝像機的 size,所以場景中必須有mainCamera
Water腳本 生成的流體對象出現(xiàn)無法消除的問題
原因: 插件在生成水滴對象時對對象的父類添加了隱藏在Hierarchy面板顯示的標簽
_parent = new GameObject(_parentNameID + gameObject.GetInstanceID());
_parent.transform.SetParent(transform);
//_parent.transform.hideFlags = HideFlags.HideInHierarchy;
WaterDropsObjects = new GameObject[DropCount];
這里我已經(jīng)注釋掉了凸舵,如果不注釋 ,可能會出現(xiàn)在頻繁測試過程中荚孵,出現(xiàn)不可消除水滴而又無法從面板找到清楚的現(xiàn)象妹窖。
解決方案:如果已經(jīng)出現(xiàn),復制現(xiàn)有場景里的對象到新的場景中,棄用以前的場景收叶。注釋掉隱藏代碼骄呼,或者添加自定義父對象。
將流體組件添加到預制體導致運行游戲時流體對象丟失引用,游戲或者測試過程中重復產(chǎn)生多個無索引流體對象
原因:因為流體對象是動態(tài)生成并綁定對象的蜓萄,所以不能添加到預制體中隅茎,在添加流體組件后,原始代碼會在編輯器Update中檢測并生成設(shè)定數(shù)量的流體對象并綁定流體組件嫉沽,在運行游戲的時候直接調(diào)用這些組件患膛。為了方便管理,將組件添加到預制體里之后耻蛇,會丟失這些引用關(guān)系踪蹬,運行游戲時,要么找不到引用臣咖,要么在退出游戲時重復生成對象跃捣。
解決方法:
修改綁定方法,判斷并刪除重復生成的對象
public void SetupParticles()
{
Debug.Log("Bind");
// if (_parent == null && WaterDropsObjects != null)
// {
// Debug.Log(1);
// if (WaterDropsObjects.Length > 0 && WaterDropsObjects[0] != null)
// _parent = WaterDropsObjects[0].transform.parent.gameObject;
// }
// if(_parent != null){
// Debug.Log(2);
// DestroyImmediate(_parent);
//
// }
Transform tran = transform.Find(_parentNameID + gameObject.GetInstanceID());
if(tran) DestroyImmediate(tran.gameObject);
_parent = new GameObject(_parentNameID + gameObject.GetInstanceID());
_parent.transform.SetParent(transform);
//_parent.transform.hideFlags = HideFlags.HideInHierarchy;
WaterDropsObjects = new GameObject[DropCount];
.........
}
同時移除EditorUpdate中自動生成綁定的檢測方法(之后測試需要手動綁定)
private void OnEditorUpdate()
{
if (Application.isPlaying)
return;
if (DropObject == null)
return;
// if (WaterDropsObjects == null || WaterDropsObjects.Length < 1)
// {
// SetupParticles();
// }
//
// if (WaterDropsObjects[0] == null)
// {
// SetupParticles();
// }
.........
}
在Start方法中添加綁定檢測 (生成預制體時調(diào)用)
private void Start()
{
StartCoroutine(StartEnumerator());
//Register
SpawnersManager.ChangeSpawnerValues(instance);
if (WaterDropsObjects == null || WaterDropsObjects.Length < 1)
{
SetupParticles();
}
if (WaterDropsObjects[0] == null)
{
SetupParticles();
}
}
Water2D 的實現(xiàn)原理-----Camera.targetTexture
通過特效相機對layer 為water 的spriterender 圖片進行紋理混合(混合shader原理后面再看)夺蛇,然后將混合后的整個紋理渲染到主攝像機里疚漆,所以兩個攝像機的size必須一致,不然就會有顯示偏差
Water2D 設(shè)置選項Flip Camera Texture
勾選此項刁赦,會鏡像翻轉(zhuǎn) tagertTexture 的顯示