您的位置:首页 > 移动开发 > Unity3D

Unity 使用 EzySlice 实现模型切割

2020-06-02 06:23 771 查看

Unity 使用 EzySlice 实现模型切割

老规矩,直接上代码:
EzySlice 插件 在下面的链接工程中。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using EzySlice;

public class Splitter_ZH : MonoBehaviour
{
//切割预制体材质
public Material _NewMaterial;

//被切割预制体数组
public  List <GameObject> _ListGamPreFab;

void Update()
{
float _Mx = Input.GetAxis("Mouse X");

transform.Rotate(0, 0, _Mx);

//当点击鼠标左键时
if (Input.GetMouseButtonDown(0))
{
//创建忽略切割对象
Collider[] _Colliders = Physics.OverlapBox(transform.position, new Vector3(4, 0.00005f , 4), transform.rotation, ~LayerMask.GetMask("Solid"));

foreach (var item in _Colliders)
{
Destroy(item);

//切割
//GameObject[] _objs GameObject[] _objs = item.gameObject.SliceInstantiate(transform.position, transform.up);

//切割出现的物体
SlicedHull _SlicedHull = item.gameObject.Slice(transform.position, transform.up);
if (_SlicedHull != null)
{
//切割下半部分部分  物体
GameObject _Lower = _SlicedHull.CreateLowerHull(item.gameObject, _NewMaterial);

//切割上半部分部分  物体
GameObject _Upper = _SlicedHull.CreateUpperHull(item.gameObject, _NewMaterial);

GameObject[] _objs = new GameObject[] { _Lower, _Upper };

for (int i = 0; i < _objs.Length; i++)
{
_objs[i].AddComponent<Rigidbody>();
_objs[i].AddComponent<MeshCollider>().convex = true;
}
}
}
}

//物体生成
if (Input.GetMouseButtonDown(1))
{
GameObject _GamPrefab = _ListGamPreFab[Random.Range(0, _ListGamPreFab.Count - 1)];

if (_GamPrefab.GetComponent<Rigidbody>())
{
GameObject _NewGamPrefab= Instantiate(_GamPrefab);
_NewGamPrefab.GetComponent<Rigidbody>().AddForce(Vector3.up * 500);

}
else
{
_GamPrefab.AddComponent<Rigidbody>();

GameObject _NewGamPrefab = Instantiate(_GamPrefab);
_NewGamPrefab.GetComponent<Rigidbody>().AddForce(Vector3.up * 500);
}
}

}
}

暂时先这样吧,如果实在看不明白就留言,看到我会回复的。
路长远兮,哈哈哈,与君共勉。

GitHub: EzySlice 工程

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: