Unity從UI拖物體到場景中以及鼠標拖動物體

當我們做一些農場遊戲或者類似可編輯戰場類遊戲時經常會遇到從UI上拖動物體在3D場景裏生成,要實現這種從UI上拖物體的效果其實挺簡單,代碼不復雜,主要是想法很重要,一個優秀的程序員不只是技術紮實,更重要的是要多思考,不思考的程序員只是一個碼農。思維活躍的人總能想到更簡單的辦法去解決一些看似複雜的問題。本次實現從UI上向場景裏拖物體的效果圖如下:

這裏寫圖片描述
具體的實現代碼很簡單,主要代碼如下:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;
using Common;
using UnityEngine.UI;

public class DragCreat : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{    
    public string sourcename;
    public string SubSystemName;
    public int SubSystemID;
    Text Name;
    GameObject go;
    public void OnBeginDrag(PointerEventData eventData)
    {
        MoseState.ChangeCameraState(MoseState.State.Bussing);
        AssetBundle bundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/SubSystem/" + sourcename + ".assetbundle");
        go = Instantiate(bundle.LoadAsset(SubSystemName)) as GameObject;
        bundle.Unload(false);
    }

    public void OnDrag(PointerEventData eventData)
    {       
        if (go!=null)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            Vector3 screenSpace;
            if (Physics.Raycast(ray, out hit))
            {
                go.transform.position = hit.point;
            }
            else
            {
                Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                go.transform.position = pos;
            }
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        MoseState.ChangeCameraState(MoseState.State.Waite);
        IfDragging = false;
        go.AddComponent<DragManage>();              
    }
    // Use this for initialization
    void Start () {
        Name = transform.FindChild("Text").GetComponent<Text>();
    }   
}

這裏有很些代碼是沒用的,這些是項目的一些片段,這裏主要提供一個思路,當開始拖拽時我們生成要生成物體的實例,拖拽進行中時我們檢測射線擊中的場景中的點然後把這個點的座標賦值給生成的物體,當射線沒有擊中場景時就把鼠標在worldspace中的座標賦值給生成的物體。
這是拖拽生成物體,當物體放置在場景中後我們想再次調整物體的位置,這時我們看用以下腳本實現:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;
using Common;
using Unity;

public class DragManage : MonoBehaviour
{
    //鼠標經過時改變物體顏色  
    private Color mouseOverColor = Color.blue;//聲明變量爲藍色  
    private Color originalColor;//聲明變量來存儲本來顏色  
    GameObject Propertygo;
    void Start()
    {
        Propertygo = GameObject.Find("Property");        
        originalColor =GetComponent<Renderer>().sharedMaterial.color;//開始時得到物體着色  
    }

    void OnMouseEnter()
    {
        MoseState.ChangeCameraState(MoseState.State.Bussing);
        GetComponent<Renderer>().material.color = mouseOverColor;//當鼠標滑過時改變物體顏色爲藍色  
    }

    void OnMouseExit()
    {
        GetComponent<Renderer>().material.color = originalColor;//當鼠標滑出時恢復物體本來顏色
        MoseState.ChangeCameraState(MoseState.State.Waite);
    }
    IEnumerator OnMouseDown()
    {     
        Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);//三維物體座標轉屏幕座標  
        //將鼠標屏幕座標轉爲三維座標,再計算物體位置與鼠標之間的距離  
        var offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));       
        while (Input.GetMouseButton(0))
        {           
            Propertygo.GetComponent<GoProperty>().SetProperty();
            MoseState.ChangeCameraState(MoseState.State.Bussing);
            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
            var curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
            transform.position = curPosition;
            StateVar.nowgameobject = this.gameObject;           
            yield return new WaitForFixedUpdate();
        }
    }
}

拖動到效果圖如下:
這裏寫圖片描述
以上就是從UI拖拽生成物體以及在場景中用鼠標拖動物體的實現方式,如果你們有更好的方法,大家可以交流!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章