2013-10-11 16:28:25 AWNUXCVBN 阅读数 6331

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
    public GUISkin skin;
    public string txt;

    private bool menuState = false;
    private Vector2 oldCursonPos;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {

    }

    void OnGUI()
    {

        GUI.skin = skin;
        txt = GUI.TextField(new Rect(100, 100, 80, 20), txt, 4);

        if (Input.GetMouseButtonDown(0))
        {
            RightMenu();
            //menuState = false;
        }

        if (Input.GetMouseButtonDown(1))
        {
            menuState = true;
            oldCursonPos = Input.mousePosition;
        }
        RightMenu();
    }

    void RightMenu()
    {
        if (menuState)
        {
            if (GUI.Button(new Rect(oldCursonPos.x, Screen.height - oldCursonPos.y, 100, 20), "复制"))
            {
                Debug.Log("复制");
                menuState = false;
            }
            if (GUI.Button(new Rect(oldCursonPos.x, Screen.height - oldCursonPos.y + 20, 100, 20), "粘贴"))
            {
                Debug.Log("粘贴");
                menuState = false;
            }
        }
    }

}



2019-12-21 15:38:04 zhunju0089 阅读数 90

需求:制作一个鼠标中键移动缩放和鼠标右键旋转的功能(鼠标右键旋转可以自行改成鼠标左键旋转)

效果图:

1.新建一个名为Target的立方体cube

2.新建一个材质球 Yellow颜色的并挂载到Target立方体上

 

3.新建一个脚本 MatrixAction.cs

4.MatrixAction.cs代码

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

public class MatrixAction : MonoBehaviour
{
    [Header("操作对象")]
    /// <summary>
    /// 操作对象
    /// </summary>
    public Transform m_target;
    void Start()
    {
        isDown1 = false;
        isDown2 = false;
    }
    /// <summary>
    /// 右键是否按下 
    /// </summary>
    private bool isDown1;

    /// <summary>
    /// 中键是否按下
    /// </summary>
    private bool isDown2;
    void Update()
    {
        // 按下了鼠标中键
        if (Input.GetMouseButtonDown(1))
            isDown1 = true;
        // 抬起了鼠标中键
        if (Input.GetMouseButtonUp(1))
            isDown1 = false;
        // 按下鼠标右键
        if (Input.GetMouseButtonDown(2))
            isDown2 = true;
        // 抬起了鼠标右键
        if (Input.GetMouseButtonUp(2))
            isDown2 = false;
        // 缩放 Input.mouseScrollDelta.y
        m_target.localScale += Time.deltaTime * m_target.localScale * Input.mouseScrollDelta.y;
    }
    private void LateUpdate()
    {
        if (isDown1)
        {
            // 计算单位时间内鼠标的偏移量
            float x = Input.GetAxis("Mouse X");
            float y = Input.GetAxis("Mouse Y");
            Vector3 rot = new Vector3(y, -x, 0);
            float spd = 10; //速度
            m_target.Rotate(spd * rot, Space.World);
        }
        if (isDown2)
        {
            // 计算单位时间内鼠标的偏移量
            float x = Input.GetAxis("Mouse X");
            float y = Input.GetAxis("Mouse Y");
            float spd = 0.4f;   //速度
            m_target.Translate(spd * new Vector3(x, y), Space.World);
        }
    }
}

5.新建一个Script空物体并将脚本MatrixAction.cs挂载上去,然后将Target拖拽到脚本上

6.为了Hierarchy层次更加清晰,新建几个空物体分隔各个组件 (可忽略)

2017-08-22 16:18:58 gaobiejiyi 阅读数 1820

在Unity Project面板右键菜单的拓展

本次主要的内容如下

  • MenuItem的应用
  • Selection.assetGUIDs的使用
  • 完整代码

using System;

namespace UnityEditor
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public sealed class MenuItem : Attribute
    {
        public string menuItem;
        public int priority;
        public bool validate;
        public MenuItem(string itemName);
        public MenuItem(string itemName, bool isValidateFunction);
        public MenuItem(string itemName, bool isValidateFunction, int priority);
    }
}

可知MenuItem最多有三个参数:
- 参数一表示MenuItem所处的路径
- 参数二表示是否有验证方法
- 参数三表示MenuItem在整个Menu中所处的优先级
PS: MenuItem的具体使用网上有很多,在这里就不一一说明

Selection.assetGUIDs的使用

Returns the guids of the selected assets. —— [ Unity Manual ]

如介绍所指,这个API会返回当前编辑器里所选的资源的GUID数组,为什么用这个API而不用Selection.objects,因为Selection.objects里面并不包含文件夹的信息,如果是要对所选文件夹进行操作的话,需要调用Selection.assetGUIDs。

完整代码

using UnityEngine;
using UnityEditor;

public class Tools {
    [MenuItem("Assets/Svn Commit")]
    static void SvnCommit()
    {
        string path = "Assets";
        string[] strs = Selection.assetGUIDs;
        if (strs != null)
        {
            path = "\"";
            for (int i = 0; i < strs.Length; i++)
            {
                if (i != 0)
                    path += "*";
                path += AssetDatabase.GUIDToAssetPath(strs[i]);
                if (AssetDatabase.GUIDToAssetPath(strs[i]) != "Assets")
                    path += "*" + AssetDatabase.GUIDToAssetPath(strs[i]) + ".meta";
            }
            path += "\"";
        }
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.FileName = "TortoiseProc.exe";
        process.StartInfo.Arguments = "/command:commit /path:" + path;
        process.Start();
    }

    [MenuItem("Assets/Svn Update")]
    static void SvnUpdate()
    {
        string path = "Assets";
        string[] strs = Selection.assetGUIDs;
        if (strs != null)
        {
            path = "\"";
            for (int i = 0; i < strs.Length; i++)
            {
                if (i != 0)
                    path += "*";
                path += AssetDatabase.GUIDToAssetPath(strs[i]);
                if (AssetDatabase.GUIDToAssetPath(strs[i]) != "Assets")
                    path += "*" + AssetDatabase.GUIDToAssetPath(strs[i]) + ".meta";
            }
            path += "\"";
        }
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.FileName = "TortoiseProc.exe";
        process.StartInfo.Arguments = "/command:update /path:" + path;
        process.Start();
    }
}
2019-03-18 18:40:50 qq_36772866 阅读数 847

当鼠标按键按下时,返回一次true,后面参数0是左键,1是右键,2是中键

if(Input.GetMouseButtonDown(0))
			Debug.Log("Pressed left click.");
		if(Input.GetMouseButtonDown(1))
			Debug.Log("Pressed right click.");
		if(Input.GetMouseButtonDown(2))
			Debug.Log("Pressed middle click.");

当鼠标按键按下时,返回true,可能多次,根据你鼠标按下的时间 ,后面参数0是左键,1是右键,2是中键

if(Input.GetMouseButton(0))
			Debug.Log("Pressed left click.");
		if(Input.GetMouseButton(1))
			Debug.Log("Pressed right click.");
		if(Input.GetMouseButton(2))
			Debug.Log("Pressed middle click.");

当鼠标按键按下后弹起时,返回一次true,后面参数0是左键,1是右键,2是中键

if(Input.GetMouseButtonUp(0))
			Debug.Log("Pressed left click.");
		if(Input.GetMouseButtonUp(1))
			Debug.Log("Pressed right click.");
		if(Input.GetMouseButtonUp(2))
			Debug.Log("Pressed middle click.");
2017-11-25 11:20:33 q764424567 阅读数 2336

在这里插入图片描述
把如下代码直接挂在摄像机对象上

using UnityEngine;
using System.Collections;
 
public class Script_07_11 : MonoBehaviour
{
 
public Transform target;
private int MouseWheelSensitivity = 1;
private int MouseZoomMin = 1;
private int MouseZoomMax = 5;
private float normalDistance = 3;
 
private Vector3 normalized;
 
private float xSpeed = 250.0f;
private float ySpeed = 120.0f;
 
private int yMinLimit = -20;
private int yMaxLimit = 80;
 
private float x = 0.0f;
private float y = 0.0f;
 
private Vector3 screenPoint;
private Vector3 offset;
 
private Quaternion rotation = Quaternion.Euler(new Vector3(30f,0f,0f));
private Vector3 CameraTarget;
void Start ()
{
 
	CameraTarget = target.position;
 
	float z = target.transform.position.z - normalDistance;
	transform.position = rotation * new Vector3(transform.position.x,transform.position.y,z);
 
	transform.LookAt(target);
 
	var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;
}
 
void Update ()
{
 
	if(Input.GetMouseButton(1))
	{
	     x += Input.GetAxis(Mouse X”) * xSpeed * 0.02f;
         y -= Input.GetAxis(Mouse Y”) * ySpeed * 0.02f;
 
		 y = ClampAngle(y, yMinLimit, yMaxLimit);
 
         var rotation = Quaternion.Euler(y, x, 0);
         var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;
 
         transform.rotation = rotation;
         transform.position = position;
 
	}
	else if (Input.GetAxis(Mouse ScrollWheel”) != 0)
	{
	    normalized = (transform.position - CameraTarget).normalized;
	}
 
	if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)
	{
		normalDistance -= Input.GetAxis(Mouse ScrollWheel”) * MouseWheelSensitivity;
	}
	if (normalDistance < MouseZoomMin)
	{
		normalDistance = MouseZoomMin;
	}
	if (normalDistance > MouseZoomMax)
	{
		normalDistance = MouseZoomMax;
	}
	
	transform.position =    normalized * normalDistance;
 
	}
	else if(Input.GetMouseButtonDown(2))
	{
	     screenPoint = Camera.main.WorldToScreenPoint(target.transform.position);
		offset = target.transform.position - Camera.main.ScreenToWorldPoint(new        Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
	}
 
	if(Input.GetMouseButton(2))
	{
		Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
 
		Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
		target.transform.position = curPosition;
	}
	transform.LookAt(CameraTarget);
 
}
 
static float ClampAngle (float angle , float min ,float  max)
{
	if (angle < -360)
	angle += 360;
	if (angle > 360)
	angle -= 360;
	return Mathf.Clamp (angle, min, max);
}

最后是工程下载地址:http://vdisk.weibo.com/s/Dorl2

没有更多推荐了,返回首页