207 lines
5.6 KiB
C#
207 lines
5.6 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Thousandto.Launcher.ExternalLibs
|
|
{
|
|
public class GrassRelationScript : MonoBehaviour
|
|
{
|
|
#region//私有变量
|
|
[SerializeField]
|
|
private ChildMeshDataScript[] _relationScripts = null;
|
|
//[SerializeField]
|
|
private Vector3[] _newVertices = null;
|
|
[SerializeField]
|
|
private Mesh _newMesh = null;
|
|
|
|
//组合的mesh的位置
|
|
[SerializeField]
|
|
private Vector3 _meshPostion = Vector3.zero;
|
|
//看到草的距离 --- 太远的就不处理了.
|
|
private const float CN_VIEW_LENGTH = 25f;
|
|
|
|
//当前摄像机
|
|
private Camera _current = null;
|
|
//当前摄像机的Transform
|
|
private Transform _cameraTrans = null;
|
|
//当前是否可以被泵装
|
|
private bool _beCanCollision = false;
|
|
#endregion
|
|
|
|
#region//static
|
|
//当前需不需要处理顶点动画
|
|
public static bool IsEnabled = false;
|
|
#endregion
|
|
|
|
#region//属性
|
|
private Transform TransformInst
|
|
{
|
|
get
|
|
{
|
|
return transform;
|
|
}
|
|
}
|
|
|
|
public Vector3[] NewVertices
|
|
{
|
|
get
|
|
{
|
|
return _newVertices;
|
|
}
|
|
|
|
set
|
|
{
|
|
_newVertices = value;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region//运行时调用
|
|
void OnBecameInvisible()
|
|
{
|
|
if (IsEnabled)
|
|
{
|
|
SetVisible(false);
|
|
}
|
|
enabled = false;
|
|
}
|
|
|
|
void OnBecameVisible()
|
|
{
|
|
if (IsEnabled)
|
|
{
|
|
SetVisible(true);
|
|
}
|
|
enabled = true;
|
|
}
|
|
|
|
void OnWillRenderObject()
|
|
{
|
|
if (IsEnabled)
|
|
{
|
|
if (_current != Camera.current)
|
|
{
|
|
_current = Camera.current;
|
|
_cameraTrans = Camera.current.transform;
|
|
}
|
|
}
|
|
}
|
|
private void Start()
|
|
{
|
|
SetVisible(IsEnabled);
|
|
if (NewVertices == null)
|
|
{
|
|
NewVertices = _newMesh.vertices;
|
|
}
|
|
}
|
|
|
|
//设置是否Enabled
|
|
public static void SetEnabled(bool isEnabled)
|
|
{
|
|
IsEnabled = isEnabled;
|
|
}
|
|
|
|
//设置可见性
|
|
public void SetVisible(bool isVisible)
|
|
{
|
|
for (int i = 0; i < _relationScripts.Length; i++)
|
|
{
|
|
_relationScripts[i].enabled = isVisible;
|
|
}
|
|
}
|
|
|
|
//设置是否可以被碰撞
|
|
private void SetBeCanCollision(bool beCanCollision)
|
|
{
|
|
if (beCanCollision != _beCanCollision)
|
|
{
|
|
_beCanCollision = beCanCollision;
|
|
SetVisible(_beCanCollision);
|
|
}
|
|
}
|
|
|
|
//更新顶点
|
|
private void UpdateVerteices(RuntimeChildMesh info)
|
|
{
|
|
for (int i = 0; i < info.NewVertices.Length; i++)
|
|
{
|
|
NewVertices[info.StartIndex + i] = info.NewVertices[i];
|
|
}
|
|
}
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (IsEnabled)
|
|
{
|
|
//设置
|
|
if (_cameraTrans == null)
|
|
{
|
|
SetBeCanCollision(false);
|
|
}
|
|
else
|
|
{
|
|
SetBeCanCollision(Vector3.Distance(_cameraTrans.position, _meshPostion) < CN_VIEW_LENGTH);
|
|
}
|
|
|
|
if (_beCanCollision)
|
|
{
|
|
bool isDirty = false;
|
|
for (int i = 0; _relationScripts != null && i < _relationScripts.Length; i++)
|
|
{
|
|
if (_relationScripts[i].ChildMeshInfo.IsDirty)
|
|
{
|
|
isDirty = true;
|
|
UpdateVerteices(_relationScripts[i].ChildMeshInfo);
|
|
}
|
|
|
|
}
|
|
if (isDirty)
|
|
{
|
|
_newMesh.vertices = NewVertices;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_beCanCollision = false;
|
|
IsEnabled = false;
|
|
_newMesh = null;
|
|
}
|
|
#endregion
|
|
|
|
#region//编辑器编辑时调用
|
|
#if UNITY_EDITOR && !FUNCELL_LAUNCHER
|
|
public void AddRelationScript(List<ChildMeshDataScript> list)
|
|
{
|
|
_relationScripts = list.ToArray();
|
|
}
|
|
|
|
public void InitCombineInfo()
|
|
{
|
|
for (int i = 0; i < _relationScripts.Length; i++)
|
|
{
|
|
var tran = _relationScripts[i].transform;
|
|
if (i == 0)
|
|
{
|
|
_meshPostion = tran.position;
|
|
}
|
|
else
|
|
{
|
|
_meshPostion = Vector3.Slerp(_meshPostion, tran.position, 0.5f);
|
|
}
|
|
var child = _relationScripts[i];
|
|
//增加小草的脚本
|
|
var grass = Thousandto.Core.Base.UnityUtils.RequireComponent<GrassAnimateScript>(child.gameObject);
|
|
grass.SetRunTimeScript(child);
|
|
}
|
|
MeshFilter mf = TransformInst.GetComponent<MeshFilter>();
|
|
if (mf != null)
|
|
_newMesh = mf.sharedMesh;
|
|
//GameCenter.GameSetting.IsEnabled(GameSettingKeyCode.EnableGrass);
|
|
}
|
|
#endif
|
|
#endregion
|
|
}
|
|
}
|