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";
}
}
如果两个物体相交那么就可以布尔了,按空格键进行布尔,其结果是你界面上设置的参数类型,在你的项目中你可以进行一系列调整。
欢迎下载