游戏蛮牛学习群(纯技术交流,不闲聊):159852603
游戏蛮牛 手机端
开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   碎碎

鼠标点击按钮任意位置,目标点显示到其位置

个人分类: C#程序代码 | 2018-10-10 16:00

[code]csharpcode:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
/// <summary>
/// 鼠标点击按钮任意位置,目标点显示到其位置
/// </summary>
public class MousePosToImg : MonoBehaviour
{
    /*UI层级分布
     * Canvas--renderMode:ScreenSpace-Camera,ScaleWithScreenSize---1920,1080
     * 右键空对象,添加MinMap脚本
     * 新建Button,删除Text,Image添加图片,Button设置transition和navigation为None
     * 新建Image,添加图片,分散对齐,和Button一样大
     * 新建Image,设置为圆形(自定义),变换颜色,作为显示点击位置的目标点
     */
    /*其他设置
     * target为总对象中Tag为Player的预制
     * Plane:随便新建一个即可,取消勾选。
     */
    public Image BtnClickImg;//Button
    public Image img;//带图片的Image
    public Transform Plane_3D;//Plane,隐藏
    public Transform traget;//Tag:Player
    public RectTransform tragetImg;//目标点的Image
    public Camera cam;
    public float offect_y = 1f;

    public Vector3 tragetInitPos;//0,1.6,5
    Vector2 clickPos;
    Vector2 iconPos;
    Vector2 texturePos;
    RectTransform clickRect;
    void Start()
    {
        tragetInitPos = new Vector3(0, 1.6f, 5);
        traget = GameObject.FindGameObjectWithTag("Player").transform;

        clickRect = BtnClickImg.GetComponent<RectTransform>();
        xBili = clickRect.sizeDelta.x / (Plane_3D.transform.localScale.x * 10);
        yBili = clickRect.sizeDelta.y / (Plane_3D.transform.localScale.z * 10);
        texture_x = clickRect.sizeDelta.x / BtnClickImg.sprite.texture.width;
        texture_y = clickRect.sizeDelta.y / BtnClickImg.sprite.texture.height;
        //Debug.Log (xBili + "--" + yBili + "--" + texture_x + "--" + texture_y);
        EventTriggerListener.Get(BtnClickImg.gameObject).onClick = Set;
        if(traget != null)
        {
            traget.position = tragetInitPos;
        }
    }

    void Update()
    {
        if(traget != null)
        {
            tragetImg.anchoredPosition = SetIcon(traget.position);
        }
    }

    void Set(GameObject a)
    {
        //Debug.Log ("点击");
        RectTransformUtility.ScreenPointToLocalPointInRectangle(clickRect, Input.mousePosition, cam, out clickPos);

        iconPos = new Vector2(clickPos.x + clickRect.sizeDelta.x / 2, clickPos.y + clickRect.sizeDelta.y / 2);

        texturePos = new Vector2(iconPos.x / Texture_x, iconPos.y / Texture_y);

        Color c = GetColor(texturePos);
        if(ColorClick(c))
        {
            //    //点击返回的永远都有   -100, -22,0  所以原点需要转换 
            traget.transform.position = SetTraget(clickPos);
        }
    }

    Color GetColor(Vector2 pos)
    {
        Color _color = BtnClickImg.sprite.texture.GetPixel((int)pos.x, (int)pos.y);
        return _color;
        // clickImg.sprite.texture.
    }
    Vector3 SetTraget(Vector2 pos)
    {
        return new Vector3(-pos.x / XBili + Plane_3D.transform.position.x, 0f + offect_y, -pos.y / YBili + Plane_3D.transform.position.z);
    }

    Vector3 SetIcon(Vector3 pos)
    {
        return new Vector2(-(pos.x - Plane_3D.transform.position.x) * XBili, -(pos.z - Plane_3D.transform.position.z) * YBili);
    }

    bool ColorClick(Color _color)
    {
        if(_color.b > 0.6 && _color.r > 0.6)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    float xBili;
    float yBili;
    float texture_x;
    float texture_y;

    public float XBili
    {
        get { return clickRect.sizeDelta.x / (Plane_3D.transform.localScale.x * 10); }
    }
    //600,400 = 32,0,32
    public float YBili
    {
        get { return clickRect.sizeDelta.y / (Plane_3D.transform.localScale.z * 10); }
    }
    // 600,400 =667,444
    public float Texture_x
    {
        get { return clickRect.sizeDelta.x / BtnClickImg.sprite.texture.width; }
    }
    public float Texture_y
    {
        get { return clickRect.sizeDelta.y / BtnClickImg.sprite.texture.height; }
    }
}

0 0

作者的其他最新博客

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

返回顶部