一:TrailRenderer模擬管道流體
效果如圖所示:

首先,先構造流體因子
構造流體因子:創建一個空物體,掛載TrailRenderer組件,創建一個材質球,命名為Trail1,將其Shader設為Mobile/Particles/Additive,然后將下面所示的紋理圖片賦值給Trail1,作為其的Particle Texture,
紋理圖片素材:


然后掛載FlowItem腳本,完整腳本如下所示:
流體因子類:FlowItem.cs
using UnityEngine;
public class FlowItem : MonoBehaviour {
public float DelayDestroy = 0.5f;
private bool _isFlowing = false;
private PipeFlow _pipeFlow;
private int _flowIndex = 0;
private int _nextIndex = 1;
private float _flowPosition = 0f;
private Coroutine _stopCoroutine;
private void Reset()
{
transform.position = _pipeFlow.FlowPath[0];
TrailRenderer[] trails = GetComponentsInChildren<TrailRenderer>();
for (int i = 0; i < trails.Length; i++)
{
trails[i].Clear();
}
ParticleSystem[] pss = GetComponentsInChildren<ParticleSystem>();
for (int i = 0; i < pss.Length; i++)
{
pss[i].SetParticles(null, 0);
}
}
private void Update()
{
if (_isFlowing)
{
_flowPosition += _pipeFlow.FlowSpeeds[_flowIndex];
transform.position = Vector3.Lerp(_pipeFlow.FlowPath[_flowIndex], _pipeFlow.FlowPath[_nextIndex], _flowPosition);
if (_flowPosition >= 1f)
{
if (_nextIndex >= _pipeFlow.FlowPath.Count - 1)
{
_isFlowing = false;
_stopCoroutine = StartCoroutine(PipeFlow.DelayExecute(() => {
Stop();
}, DelayDestroy));
}
else
{
_flowIndex += 1;
_nextIndex = _flowIndex + 1;
_flowPosition = 0f;
}
}
}
}
public void Shoot(PipeFlow pipeFlow)
{
_pipeFlow = pipeFlow;
Reset();
gameObject.SetActive(true);
_isFlowing = true;
_flowIndex = 0;
_nextIndex = 1;
_flowPosition = 0f;
if (_stopCoroutine != null)
{
StopCoroutine(_stopCoroutine);
_stopCoroutine = null;
}
}
public void Stop()
{
gameObject.SetActive(false);
_isFlowing = false;
if (!_pipeFlow.Items.Contains(this))
{
_pipeFlow.Items.Add(this);
}
if (_stopCoroutine != null)
{
StopCoroutine(_stopCoroutine);
_stopCoroutine = null;
}
}
}
最后將其做成預制體即可,
然后可以看下我TrailRenderer的屬性設定,FlowItem的Delay Destroy屬性為此流體因子延時消亡的時間,最好跟TrailRenderer的Time屬性保持一致,這樣的話才不會看到整條TrailRenderer突然消失的情況:

每一個流體因子攜帶一個拖尾渲染器,由PipeFlow根據其屬性FlowInterval(間隔發射時間)進行持續發射(如果不是OnlyOnce模式),每一個流體因子從路徑起點抵達路徑終點的時間為FlowTime,
//PipeFlow.cs
public void Flow(Action endAction);
外部呼叫Flow方法為開啟流體,引數endAction當第一個流體因子抵達管道路徑終點時觸發,可以為空,
其次,實作管道路徑
因為TrailRenderer可以通過設定Corner Vertices(拐角處頂點數量)來自動圓角,所以不用考慮使用任何曲線演算法,這樣還能保證我們的路徑點絕對的貼合管道,畢竟管道模型可能會有你意想不到的彎曲復雜度,
不過我們為了要保證流體在管道的每一個位置都保持相同速度流動,所以必須為每一個路段指定不同的流動速度,
管道流體實體類:PipeFlow.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PipeFlow : MonoBehaviour {
public FlowItem ItemTemplate;
public float FlowTime = 1f;
public float FlowInterval = 0.5f;
public bool OnlyOnce = false;
[HideInInspector]
public List<Vector3> FlowPath = new List<Vector3>();
[HideInInspector]
public List<float> FlowSpeeds = new List<float>();
[HideInInspector]
public List<FlowItem> Items = new List<FlowItem>();
private bool _isInit = false;
private bool _isFlowing = false;
private float _flowInterval = 0f;
private Action _actionTrigger;
private Coroutine _actionCoroutine;
private void Awake()
{
if (!_isInit)
{
Init();
}
}
private void Init()
{
float tatol = 0f;
for (int i = 0; i < FlowPath.Count - 1; i++)
{
tatol += Vector3.Distance(FlowPath[i], FlowPath[i + 1]);
}
for (int i = 0; i < FlowPath.Count - 1; i++)
{
float dis = Vector3.Distance(FlowPath[i], FlowPath[i + 1]);
float time = dis / tatol * (FlowTime * 50);
FlowSpeeds.Add(1f / time);
}
_isInit = true;
}
private void Update()
{
if (_isFlowing)
{
_flowInterval += Time.deltaTime;
if (_flowInterval >= FlowInterval)
{
_flowInterval = 0f;
ShootItem();
if (OnlyOnce)
{
_isFlowing = false;
}
}
}
}
private void ShootItem()
{
if (Items.Count > 0)
{
Items[0].Shoot(this);
Items.RemoveAt(0);
}
else
{
GameObject item = Instantiate(ItemTemplate.gameObject);
item.transform.parent = transform;
item.GetComponent<FlowItem>().Shoot(this);
}
}
public void Flow(Action endAction)
{
if (FlowPath.Count < 2)
{
Debug.LogWarning("路徑點數量必須大于等于2!");
return;
}
if (!ItemTemplate)
{
Debug.LogWarning("ItemTemplate不能為空!");
return;
}
if (!_isInit)
{
Init();
}
_isFlowing = true;
_flowInterval = FlowInterval;
_actionTrigger = endAction;
if (_actionCoroutine != null)
{
StopCoroutine(_actionCoroutine);
_actionCoroutine = null;
}
if (_actionTrigger != null)
{
_actionCoroutine = StartCoroutine(DelayExecute(_actionTrigger, FlowTime));
}
}
public void Stop()
{
_isFlowing = false;
if (_actionCoroutine != null)
{
StopCoroutine(_actionCoroutine);
_actionCoroutine = null;
}
FlowItem[] fis = transform.GetComponentsInChildren<FlowItem>();
foreach (FlowItem fi in fis)
{
fi.Stop();
}
}
public static IEnumerator DelayExecute(Action action, float delaySeconds)
{
yield return new WaitForSeconds(delaySeconds);
action();
}
}
該類的編輯器重寫:PipeFlowEditor.cs
這個編輯器方法主要是實作在面板上可視化、自定義編輯爐體路徑點功能,此類腳本不需要被掛載在某個物體上,
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(PipeFlow)), CanEditMultipleObjects]
public class PipeFlowEditor : Editor
{
private PipeFlow _pipeFlow;
private int _currentIndex = -1;
private bool _showInEditor = true;
private void OnEnable()
{
_pipeFlow = target as PipeFlow;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
EditorGUILayout.BeginVertical("HelpBox");
EditorGUILayout.BeginHorizontal();
GUILayout.Label("FlowPath");
_showInEditor = GUILayout.Toggle(_showInEditor, "Show In Editor");
EditorGUILayout.EndHorizontal();
if (_showInEditor)
{
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("路徑倒置", "ButtonLeft"))
{
if (EditorUtility.DisplayDialog("提示", "是否將整條路徑倒置?", "是的", "我再想想"))
{
if (_pipeFlow.FlowPath.Count > 1)
{
_pipeFlow.FlowPath.Reverse();
}
}
}
if (GUILayout.Button("清空路徑點", "ButtonRight"))
{
if (EditorUtility.DisplayDialog("提示", "是否清空路徑點?", "是的", "我再想想"))
{
_pipeFlow.FlowPath.Clear();
_currentIndex = -1;
}
}
EditorGUILayout.EndHorizontal();
for (int i = 0; i < _pipeFlow.FlowPath.Count; i++)
{
EditorGUILayout.BeginHorizontal();
GUI.backgroundColor = _currentIndex == i ? Color.cyan : Color.white;
if (GUILayout.Button("path point" + (i + 1), "prebutton"))
{
_currentIndex = i;
Tools.current = Tool.None;
}
GUI.backgroundColor = Color.white;
if (GUILayout.Button("", "OL Minus", GUILayout.Width(16)))
{
_pipeFlow.FlowPath.RemoveAt(i);
_currentIndex = -1;
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("", "OL Plus", GUILayout.Width(16)))
{
if (_currentIndex != -1)
{
_pipeFlow.FlowPath.Add(_pipeFlow.FlowPath[_currentIndex]);
}
else
{
_pipeFlow.FlowPath.Add(new Vector3(0, 0, 0));
}
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndVertical();
}
private void OnSceneGUI()
{
if (_showInEditor)
{
Handles.color = Color.cyan;
if (_pipeFlow.FlowPath.Count > 0)
{
Handles.Label(_pipeFlow.FlowPath[0], "[" + _pipeFlow.transform.name + "]起點", "ErrorLabel");
}
if (_pipeFlow.FlowPath.Count > 1)
{
Handles.Label(_pipeFlow.FlowPath[_pipeFlow.FlowPath.Count - 1], "[" + _pipeFlow.transform.name + "]終點", "ErrorLabel");
}
for (int i = 0; i < _pipeFlow.FlowPath.Count; i++)
{
//每一個操作手柄添加位移功能
_pipeFlow.FlowPath[i] = Handles.PositionHandle(_pipeFlow.FlowPath[i], Quaternion.identity);
//重新為每一個操作手柄添加序號標簽(i+1)可以在scene看到從1開始,與監視面板的“path point1”對應
Handles.Label(_pipeFlow.FlowPath[i] + new Vector3(0, 0.025f, 0), "[" + (i + 1) + "]", "ErrorLabel");
if (i < _pipeFlow.FlowPath.Count - 1)
Handles.DrawLine(_pipeFlow.FlowPath[i], _pipeFlow.FlowPath[i + 1]);
}
if (_currentIndex != -1)
{
_pipeFlow.FlowPath[_currentIndex] = Handles.PositionHandle(_pipeFlow.FlowPath[_currentIndex], Quaternion.identity);
}
}
}
}
創建好上面兩個腳本類之后,然后在unity中創建一個空物體,掛載PipeFlow腳本,然后將上面所創建的流體因子賦值,設定好合適的時間引數,最后,搭建路徑點即可,如圖所示:

最后,再強調一下外部呼叫PipeFlow類里Flow()方法為開啟流體,引數endAction當第一個流體因子抵達管道路徑終點時觸發,可以為空;呼叫PipeFlow里Stop()方法為停止流體,
二:箭頭指引效果
效果圖如下:

用到的素材如下:

實作步驟如下:
首先:創建一個Cube,然后將如下腳本掛載在Cube上,腳本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UVMoves : MonoBehaviour
{
public float ScrollSpeed = 10;
public int countX = 1;
public float countY = 2;
private float offsetX = 0.0f;
private float offsetY = 0.0f;
// private GameObject singleTexSize;
// Use this for initialization
void Start()
{
float x_1 = 1.0f / countX;
float y_1 = 1.0f * countY;
GetComponent<Renderer>().material.mainTextureScale = new Vector2(x_1, y_1);
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
float frame = (Time.time * ScrollSpeed);
//offsetX = frame / countX;
//水平方向運動
//offsetY = -(frame - frame % countX) / countY / countX;
//offsetX = frame / countX;
//垂直方向運動
offsetY = frame / countY;
offsetX = -(frame - frame % countY) / countY / countX;
GetComponent<Renderer>().material.SetTextureOffset("_MainTex", new Vector2(offsetX, -offsetY));
}
}
然后,創建一個材質球,Shader型別選擇為Unlit/Transparent,然后將箭頭的圖片素材賦值上去,如下圖所示:

然后,將該材質賦值給創建的Cube上,即可,
原理:這種方法就是通過代碼控制貼圖的UV以一定的速度朝著一定的方向運動,
上述兩種方法,程序都比較詳細,素材資源也都提供了,基本上可以自行實作,如還不能實作的,可下載這個原工程檔案:
https://download.csdn.net/download/qq_44718259/21749562?spm=1001.2014.3001.5501
暫時就這兩個方法,后續接觸到新的流體效果實作方式再更新,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/336603.html
標籤:其他
