内容纲要

RenderToTextur (渲染到纹理)

步骤:

  1. 建立一个Canvas控件
  2. 在Canvas控件下建立一个RawImage控件
  3. 在RawImage控件下建立LeftButton、RightButton两个控件
  4. 添加所需要渲染的模型控件
  5. 在模型控件上挂载RobotRotate脚本
  6. 将相应的控件拖入脚本中
  7. 在两个Button控件中On Click方法调试成想要的方法

渲染纹理和物品拖放插图

脚本代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RobotRotate : MonoBehaviour
{
    public Button leftButton;
    public Button rightButton;

    // Update is called once per frame
    void Update()
    {
        //transform.Rotate(Vector3.up, Time.deltaTime * 60);
    }

    public void LeftButtonDown() { transform.Rotate(Vector3.up); }

    public void RightButtonDown() { transform.Rotate(Vector3.down); }
}

物品拖放:

  • 原理:
    • 拖的部分:
      • 初始化:生成一个icon临时对象,专门用来显示要拖拽的图片UI
      • 开始拖:记录鼠标相对于icon的原点(pivot)的偏移
      • 拖动中:设置icon的位置为鼠标光标位置
      • 松开后:icon归位
    • 放的部分:
      • 通过事件参数取得放的GameObject,并进一步取得对应的图片sprite和color,设置给目标GameObject

鼠标坐标转换

由屏幕的像素坐标转换成世界坐标

Vector3 需要转换的坐标;
RectTransformUtility.ScreenPointToWorldPointInRectangle
(需要转到的坐标系(transform) as RectTransform,
屏幕上的坐标,
用什么摄像机(eventData.pressEventCamera),
out 得到的坐标);

相关代码:

  • DrapImage:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class DragImage : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    private GameObject miss_icon;
    private GameObject miss_canvas;
    private Vector3 miss_offset;    //以图片中心 为终点,以鼠标位置为起点向量

    public void OnBeginDrag(PointerEventData eventData)
    {
        Vector3 cursorPos, imgPos;
        RectTransformUtility.ScreenPointToWorldPointInRectangle(miss_canvas.transform as RectTransform, transform.position, eventData.pressEventCamera, out imgPos );
        RectTransformUtility.ScreenPointToWorldPointInRectangle(miss_canvas.transform as RectTransform, eventData.position, eventData.pressEventCamera, out cursorPos);
        miss_offset = imgPos - cursorPos;
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector3 cursorPos;
        RectTransformUtility.ScreenPointToWorldPointInRectangle(miss_canvas.transform as RectTransform, eventData.position, eventData.pressEventCamera, out cursorPos);

        miss_icon.transform.position = cursorPos + miss_offset;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        miss_icon.transform.position = transform.position;
    }

    // 当对象已启用并处于活动状态时调用此函数
    private void OnEnable()
    {
        miss_icon = new GameObject("icon"); //新建一个名为icon的控件
        miss_canvas = GameObject.Find("Canvas");  //找到根目录下的名为Canvas的根控件

        miss_icon.transform.parent = miss_canvas.transform; //将canvas设置为icon的父节点:miss_icon.transform.SetParent(miss_canvas.transform)

        miss_icon.transform.position = transform.position;  //将DragImage的位置设置给icon

        var miss_icon_image = miss_icon.AddComponent<Image>();  //给icon添加Image组件
        miss_icon_image.sprite = GetComponent<Image>().sprite;  //将DragImage图片设置给icon

        miss_icon.AddComponent<CanvasGroup>().blocksRaycasts = false;   //防止icon图片阻挡鼠标点击其覆盖的DragImage
    }
}
  • Dropimage:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class MyDorpImage : MonoBehaviour, IDropHandler
{
    public void OnDrop(PointerEventData eventData)
    {
        GetComponent<Image>().sprite = eventData.pointerDrag.GetComponent<Image>().sprite;
        GetComponent<Image>().color = eventData.pointerDrag.GetComponent<Image>().color;
    }
}

发表评论