Unity使用EzySlice實(shí)現(xiàn)模型多邊形順序切割
Unity使用EzySlice實(shí)現(xiàn)模型切割,供大家參考,具體內(nèi)容如下
老規(guī)矩,直接上代碼:
注意:腳本搭載和需要的材質(zhì)球以及切割數(shù)組填充
EzySlice 多邊形順序切割
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EzySlice;
public class SplitterModel_ZH : MonoBehaviour
{
//切割預(yù)制體材質(zhì)
public Material _NewMaterial;
//被切割預(yù)制體數(shù)組
public List<GameObject> _ListGamPreFab;
//調(diào)用切割模型數(shù)組 序號(hào)
private int _ListInt = 0;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartCoroutine(SlicedModel());
}
}
public IEnumerator SlicedModel()
{
if (_ListGamPreFab != null)
{
//創(chuàng)建忽略切割對(duì)象
Collider[] _Colliders = Physics.OverlapBox(_ListGamPreFab[_ListInt].transform.position, new Vector3(4, 0.00005f, 4), _ListGamPreFab[_ListInt].transform.rotation, ~LayerMask.GetMask("Solid"));
foreach (var item in _Colliders)
{
//銷(xiāo)毀當(dāng)前被切割物體
Destroy(item.gameObject);
//切割出現(xiàn)的物體
SlicedHull _SlicedHull = item.gameObject.Slice(_ListGamPreFab[_ListInt].transform.position, _ListGamPreFab[_ListInt].transform.up);
if (_SlicedHull != null)
{
//切割下半部分部分 物體
GameObject _Lower = _SlicedHull.CreateLowerHull(item.gameObject, _NewMaterial);
//切割上半部分部分 物體
GameObject _Upper = _SlicedHull.CreateUpperHull(item.gameObject, _NewMaterial);
//銷(xiāo)毀切割形成的上半部分
Destroy(_Lower);
//添加網(wǎng)格組件
_Upper.AddComponent<MeshCollider>();
//當(dāng)前切割物體消失(可擴(kuò)展)
_ListGamPreFab[_ListInt].gameObject.SetActive(false);
#region 棄用
//for (int i = 0; i < _objs.Length; i++)
//{
// _objs[i].AddComponent<Rigidbody>();
// _objs[i].AddComponent<MeshCollider>().convex = true;
// //奇 偶 判斷 如果是奇數(shù)
// if ((i & 1) != 0)
// {
// }
//}
#endregion
}
}
}
_ListInt++;
//延遲執(zhí)行
yield return new WaitForSeconds(0.5f);
//判斷數(shù)組大小
if (_ListInt == _ListGamPreFab.Count)
{
//停止協(xié)程
StopCoroutine(SlicedModel());
}
else
{
StartCoroutine(SlicedModel());
}
}
}
補(bǔ)充一點(diǎn):當(dāng)前切割數(shù)組可擴(kuò)展,可以使用 LineRender 繪畫(huà)實(shí)現(xiàn)自定義,只不過(guò)我沒(méi)時(shí)間去寫(xiě)。
如果有那位大神寫(xiě)了請(qǐng)幫忙踢我一下,哈哈哈。
初始狀態(tài):

腳本搭載情況:

最終效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小功能
C#中結(jié)構(gòu)(struct)的部分初始化和完全初始化實(shí)例分析
.NET連接MongoDB數(shù)據(jù)庫(kù)實(shí)例教程

