Unity3D click on the UI picture to generate model presets

                        Unity3D click on the UI picture to generate model presets


table of Contents

1. Blog introduction

2. Content

(1) Demo

(2) Generation

(3) Generate object movement

 3. Push

4. Conclusion


1. Blog introduction

I turned out the previous Demo two years ago and passed it up. The model presets are generated by clicking buttons or other UI elements and moving with the mouse.


2. Content

(1) Demo

(2) Generation

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ClickButton : MonoBehaviour {

    public GameObject Cube;

    public GameObject Target;

    private Button _btnCreate;


	void Start () {

	    _btnCreate = GameObject.Find("Button").GetComponent<Button>();
		_btnCreate.AddListener(EventTriggerType.PointerDown,OnDownButton);

	}

    public void OnDownButton(BaseEventData baseEventData)
    {
        GameObject cubePre = Instantiate(Cube);
	    if (Camera.main != null)
	    {
		    Vector3 targetSpace = Camera.main.WorldToScreenPoint(Target.transform.position);
		    cubePre.transform.position= Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetSpace.z));
	    }
    }
}

When generating the script, the only point to note is that when the screen coordinates of the mouse are converted to world coordinates, the Z value selects an object in the scene as the reference value. The coordinates of the object must be converted to screen coordinates, and then the rotated Z The value combination mouse click position is converted into world coordinates.

(3) Generate object movement

using UnityEngine;
using System.Collections;

public class CubeDragScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
       
        StartCoroutine(OnMouseDown());
    }
	

    IEnumerator OnMouseDown()
    {
        if (Camera.main != null)
        {
            //三维物体世界坐标转屏幕坐标  
            Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);

            //完成两个步骤 1.由于鼠标的坐标系是2维,需要转换成3维的世界坐标系  

            //            2.只有3维坐标情况下才能来计算鼠标位置与物理的距离,offset即是差值

            //将鼠标屏幕坐标转为世界三维坐标,转换前的屏幕Z值套用Cube转换过得Z值,再算出Cube世界坐标与鼠标世界坐标的差值

            Vector3 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
        
            while (Input.GetMouseButton(0))

            {
           
                Camera.main.GetComponent<MouseOrbit>().enabled = false;
                
                //得到现在鼠标的屏幕坐标
                Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);

                //物体的位置就是鼠标的世界坐标 + 差值
                Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;

                transform.position = curPosition;
                //这个很重要,循环执行
                yield return null; 

            }
        }

        if (Camera.main != null) Camera.main.GetComponent<MouseOrbit>().enabled = true;
    }



}

The comments are very clear, just look at the code. The MouseOrbit script controls the rotation of the lens and is disabled when dragging the object. The source code and the demo scene are required to jump to the blogger Github.


 3. Push

工程Github:https://github.com/KingSun5/Demo_UICreateObj


4. Conclusion

       If you feel that the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the article, you are welcome to comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

       This article is an original article, please reprint the source of the famous author and stick to the top! ! ! !

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/94507384