[Unity]建造放置系统BuildSystem




Build System


下载完毕,导入。





下面这个脚本,直接挂载到一个空物体,新建一个 3D物体 立方体,将其设置为 BuildGameObject变量的值(如下图所示)。新建一个 按钮button,来触发placeBool函数即可 实现放置基本的物体。


using UnityEngine;
using BuildSystem;


public class BuildManage : MonoBehaviour {


    public Material ghostMaterial;//放置物体 时候的 材质
    private Material goMaterial;


    //**********************************************************************************************
    [Header("Input Settings")]


    [Tooltip("Key to press to enable the builder mode. This is also used by ObjectSelector")]
    public KeyCode toggleKey = KeyCode.E;


    [Tooltip("Key to press to place a item in the scene")]
    public KeyCode placeKey = KeyCode.Mouse0;


    [Tooltip("Key to press rotate (forward) the object based on snapRotaionDeg")]
    public KeyCode positiveRotateKey = KeyCode.Mouse1;


    [Tooltip("Key to press rotate (backward) the object based on snapRotaionDeg")]
    public KeyCode negativeRotateKey = KeyCode.None;


    [Header("More Settings")]
    [Tooltip("Disable the remover script when placer is active. Note: remover must be next to this script")]
    public bool shouldToggleRemover = true;


    [Tooltip("Max distance from camera where you can place objects")]
    public float maxPlaceDistance = 100f;
    /****************************************************
    * Editor Interface
    * *************************************************/


    [Tooltip("Camera used to raycast and find place position, if empty the script will try to use the main camera")]
    public Camera cam;




    public LayerMask movementMask;//设置 LayerMask 
    public GameObject buildGameObject;//建设的物体


    /****************************************************
    * Variables
    * *************************************************/


    bool isActive = true;


    bool canPlace = false;


    bool mouseIsNotOnUI = true;


    //object rotation
    float objectSnapCurrentRotaion = 0;


    //object pivot
    bool usingFakePivot = false;


    /****************************************************
    * Events
    * *************************************************/


    public delegate void BuildEvent();


    public event BuildEvent OnGhostObjectCreation;
    public event BuildEvent OnGhostObjectDestroy;
    public event BuildEvent OnGhostObjectPlace;
    /****************************************************
        * Components & references
      &
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值