Unity Boolean CSG插件的使用

Boolean

目录

​编辑

物体及摄像机控制

物体控制

相机控制

UI切换布尔类型

调用布尔函数进行boolean


物体及摄像机控制

WASD控制物体移动,鼠标左键控制摄像机移动,右键旋转,中键远近调整

物体控制

using UnityEngine;

public class Control : MonoBehaviour
{
    bool bTab = false;
    Vector3 Axle = Vector3.zero;
    public float moveSpeed = 2f;
    float HigihtSpeed = 2f;
    public float rotateSpeed = 20f;
    float h = 0;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Tab))
            bTab = !bTab;
        var x = Input.GetAxisRaw("Horizontal");
        var z = Input.GetAxisRaw("Vertical");

        if (Input.GetKey(KeyCode.H))
        {
          
            h = bTab == true ? HigihtSpeed : -HigihtSpeed;
        }
        else
            h = 0;


        transform.Translate(new Vector3(x, h, z)* moveSpeed * Time.deltaTime);
      
        Axle = bTab == true ? Vector3.up : Vector3.down;
        if (Input.GetKey(KeyCode.Q))
            transform.Rotate(Axle* rotateSpeed * Time.deltaTime);
       

        if (Input.GetKeyDown(KeyCode.O))
        { 
        //
        }
    }
}

相机控制

using UnityEngine;
using UnityEngine.EventSystems;

public class DebugCamera : MonoBehaviour
{
    [SerializeField] float speedXY         = 0.5f;
    [SerializeField] float speedXZ         = 3f;
    [SerializeField] float speedForward    = 5f;
    [SerializeField] float angleSpeed      = 3f;

    private Vector3 preMousePosition_;
    private Vector3 targetEuler_;
    private Vector3 targetPos_;

    void Start()
    {
        preMousePosition_ = Input.mousePosition;
        targetPos_ = transform.position;
        targetEuler_ = transform.rotation.eulerAngles;
    }

    void Update()
    {
        UpdateXY();
        UpdateXZ();
        UpdateForward();
        UpdateRotation();
        UpdateMousePosition();
        UpdateTransform();
    }

    void UpdateXY()
    {
        if (Input.GetMouseButton(2) && !Input.GetMouseButtonDown(2)) {
            var dPos = Input.mousePosition - preMousePosition_;
            var velocity = ((-transform.up) * dPos.y + (-transform.right) * dPos.x) * speedXY;
            targetPos_ += velocity * Time.deltaTime;
        }
    }

    void UpdateXZ()
    {
        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
            return;
        var x = Input.GetAxisRaw("Horizontal");
        var z = Input.GetAxisRaw("Vertical");

        var velocity = Quaternion.AngleAxis(targetEuler_.y, Vector3.up) * (new Vector3(x, 0f, z)) * speedXZ;
        targetPos_ += velocity * Time.deltaTime;
    }

    void UpdateForward()
    {
        var x = Input.mouseScrollDelta.x;
        var z = Input.mouseScrollDelta.y;

        var velocity = (transform.forward * z + transform.right * x) * speedForward;
        targetPos_ += velocity * Time.deltaTime;
    }

    void UpdateRotation()
    {
        if ((Input.GetMouseButton(0) && !Input.GetMouseButtonDown(0)) ||
            (Input.GetMouseButton(1) && !Input.GetMouseButtonDown(1))) {
            var dPos = Input.mousePosition - preMousePosition_;
            targetEuler_ += new Vector3(-dPos.y, dPos.x, 0f) * Time.deltaTime * angleSpeed;
            //targetEuler_.x = Mathf.Clamp(targetEuler_.x, -90f, 90f);
        }
    }

    void UpdateMousePosition()
    {
        preMousePosition_ = Input.mousePosition;
    }

    void UpdateTransform()
    {
        transform.position = Vector3.Lerp(transform.position, targetPos_, 0.5f);
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(targetEuler_), 0.5f);
    }
}

UI切换布尔类型

using System;
using UnityEngine;
using UnityEngine.UI;

public class CSGDropdown : MonoBehaviour
{
     Dropdown m_Dropdown;
    int lastIndex = 0;
    Action<int> action;
    void Start()
    {
        InitDorpDown();
    }

    private void InitDorpDown()
    {
        m_Dropdown = GetComponent<Dropdown>();
        m_Dropdown.onValueChanged.AddListener(OnChanged);
    }

    private void OnChanged(int index)
    {
        if (index == lastIndex) return;
        lastIndex = index;
        if(action!=null)
            action(lastIndex);
        Debug.Log(index);
    }
    public void OnAction(Action<int> _action) {
        action -=_action;
        action += _action;
    }
}

通过把UI界面获取到的点击参数通过回调发给订阅者

调用布尔函数进行boolean

using UnityEngine;

public class CSGUsageExample : MonoBehaviour
{
    public GameObject lhs;

    public GameObject rhs;

    public CSG.BooleanOp Operation;

    public Material material;

    CSGDropdown cSGDropdown;
    void Start()
    {
        cSGDropdown = GameObject.FindObjectOfType<CSGDropdown>();
        cSGDropdown.OnAction((index) => {
            Operation = (CSG.BooleanOp)index;
        });
        //Perform();
    }
    void Update() {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Perform();
        }
        
    }
    public void Perform()
    {
        Model result = CSG.Perform(Operation, lhs, rhs);
        var composite = new GameObject();
        composite.AddComponent<MeshFilter>().sharedMesh = result.mesh;
        result.materials.Add(material);
        composite.AddComponent<MeshRenderer>().sharedMaterials = result.materials.ToArray();
        composite.name = Operation.ToString() + " Object";
    }
}

如果两个物体相交那么就可以布尔了,按空格键进行布尔,其结果是你界面上设置的参数类型,在你的项目中你可以进行一系列调整。

欢迎下载

UnityBooleanCSG插件的使用资源-CSDN文库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w-白兰地

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值