zoukankan      html  css  js  c++  java
  • NGUIJoysticK

    原始的:

    using UnityEngine;
    using System.Collections;
    
    public class NGUIJoystick : MonoBehaviour
    {
        public float radius = 2.0f;
        public Vector3 scale = Vector3.one;
        private Plane mPlane;
        private Vector3 mLastPos;
        private UIPanel mPanel;
        private Vector3 center;
        [HideInInspector]
        public Vector2 position;
        
        private void Start ()
        {
            center = transform.localPosition;
        }
        
        /// <summary>
        /// Create a plane on which we will be performing the dragging.
        /// </summary>
        
        private void OnPress (bool pressed)
        {
            if (enabled && gameObject.activeInHierarchy) {
                if (pressed) {
                    mLastPos = UICamera.lastHit.point;
                    mPlane = new Plane (Vector3.back, mLastPos);
                } else {
                    transform.localPosition = center;
                    position=Vector2.zero;
                }
            }
        }
    
        /// <summary>
        /// Drag the object along the plane.
        /// </summary>
    
        void OnDrag (Vector2 delta)
        {
            if (enabled && gameObject.activeInHierarchy) {
                UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;
    
                Ray ray = UICamera.currentCamera.ScreenPointToRay (UICamera.currentTouch.pos);
                float dist = 0f;
    
                if (mPlane.Raycast (ray, out dist)) {
                    Vector3 currentPos = ray.GetPoint (dist);
                    Vector3 offset = currentPos - mLastPos;
                    mLastPos = currentPos;
    
                    if (offset.x != 0f || offset.y != 0f) {
                        offset = transform.InverseTransformDirection (offset);
                        offset.Scale (scale);
                        offset = transform.TransformDirection (offset);
                    }
                    
                    offset.z = 0;
                    transform.position += offset;
                    
                    float length = transform.localPosition.magnitude;
                     
                    if (length > radius) {
                        transform.localPosition = Vector3.ClampMagnitude (transform.localPosition, radius);
                    }
    
                    position = new Vector2((transform.localPosition.x-center.x)/radius,(transform.localPosition.y-center.y)/radius);
                }
            }
        }
        
    }

    超凡特工的:

    using System;
    using UnityEngine;
    
    public class NGUIJoystick : MonoBehaviour
    {
        public bool activeFlag;
        public Vector3 center;
        private float deadRadius;
        public float deadRadiusRatio = 0.3f;
        public bool lastPressed;
        protected UIPanel mPanel;
        protected Plane mPlane;
        public Vector2 normalizedPosition;
        public float normalizedRadius = 2f;
        public Vector2 position;
        public float radius = 2f;
        public Vector3 worldCenter;
    
        internal void Move(Vector3 offset)
        {
            offset.z = 0f;
            base.transform.position = this.worldCenter + offset;
            float magnitude = base.transform.localPosition.magnitude;
            if (magnitude < this.deadRadius)
            {
                this.normalizedPosition = Vector2.zero;
            }
            else
            {
                if (magnitude > this.radius)
                {
                    base.transform.localPosition = Vector3.ClampMagnitude(base.transform.localPosition, this.radius);
                }
                float num2 = base.transform.localPosition.x - this.center.x;
                float num3 = base.transform.localPosition.y - this.center.y;
                this.position.x = num2 / this.radius;
                this.position.y = num3 / this.radius;
                this.normalizedPosition.x = num2 / this.normalizedRadius;
                this.normalizedPosition.y = num3 / this.normalizedRadius;
                this.normalizedPosition.x = Mathf.Clamp(this.normalizedPosition.x, -1f, 1f);
                this.normalizedPosition.y = Mathf.Clamp(this.normalizedPosition.y, -1f, 1f);
            }
        }
    
        internal virtual void OnDrag(Vector2 delta)
        {
            if (base.enabled && base.gameObject.activeInHierarchy)
            {
                Ray ray = UICamera.currentCamera.ScreenPointToRay((Vector3) UICamera.currentTouch.pos);
                float enter = 0f;
                if (this.mPlane.Raycast(ray, out enter))
                {
                    Vector3 offset = ray.GetPoint(enter) - this.worldCenter;
                    this.Move(offset);
                }
            }
        }
    
        internal virtual void OnPress(bool pressed)
        {
            if (base.enabled && base.gameObject.activeInHierarchy)
            {
                if (pressed)
                {
                    this.mPlane = new Plane(Vector3.back, UICamera.lastHit.point);
                }
                else
                {
                    base.transform.localPosition = this.center;
                    this.position = Vector2.zero;
                    this.activeFlag = true;
                }
                this.lastPressed = pressed;
            }
        }
    
        public void SetRadius(float r, float nr)
        {
            this.radius = r;
            this.normalizedRadius = nr;
            this.deadRadius = this.radius * this.deadRadiusRatio;
        }
    
        internal virtual void Start()
        {
            this.center = base.transform.localPosition;
            this.worldCenter = base.transform.position;
        }
    }
    namespace Kola
    {
        using LuaInterface;
        using System;
        using UnityEngine;
    
        public class KJoystick : NGUIJoystick
        {
            public UIRect bgObject;
            public bool FollowPressPosition;
            private Vector3 originPos;
            private Transform parent;
            public LuaTable playerControl;
            public LuaFunction playerControlSetInputFunc;
            public KMoveBehavior playerMove;
            public float pressAlpha = 0.5f;
    
            private void _OnDrag(GameObject go, Vector2 delta)
            {
                this.OnDrag(delta);
            }
    
            private void _OnPress(GameObject go, bool pressed)
            {
                this.OnPress(pressed);
                if (pressed)
                {
                    Vector3 point = UICamera.lastHit.point;
                    if (this.FollowPressPosition)
                    {
                        this.parent.position = point;
                    }
                    else
                    {
                        Vector3 position = this.parent.position;
                        Vector3 offset = point - position;
                        base.Move(offset);
                        this.OnDrag(Vector2.zero);
                    }
                    if (this.bgObject != null)
                    {
                        this.bgObject.alpha = this.pressAlpha;
                    }
                }
                else
                {
                    if (this.playerMove != null)
                    {
                        this.playerMove.Stand();
                    }
                    if (this.bgObject != null)
                    {
                        this.bgObject.alpha = 1f;
                    }
                    if (this.playerControlSetInputFunc != null)
                    {
                        object[] args = new object[] { this.playerControl, 0f, 0f, true };
                        KLuaFunc.Call(this.playerControlSetInputFunc, args);
                    }
                    if (this.FollowPressPosition)
                    {
                        this.parent.position = this.originPos;
                    }
                }
            }
    
            public void Attach(GameObject draggedObj, GameObject bgObj)
            {
                UIEventListener listener1 = UIEventListener.Get(draggedObj);
                listener1.onPress = (UIEventListener.BoolDelegate) Delegate.Combine(listener1.onPress, new UIEventListener.BoolDelegate(this._OnPress));
                UIEventListener listener2 = UIEventListener.Get(draggedObj);
                listener2.onDrag = (UIEventListener.VectorDelegate) Delegate.Combine(listener2.onDrag, new UIEventListener.VectorDelegate(this._OnDrag));
                this.bgObject = (bgObj != null) ? bgObj.GetComponent<UIRect>() : null;
            }
    
            private void OnDisable()
            {
                this._OnPress(base.gameObject, false);
            }
    
            internal override void OnDrag(Vector2 delta)
            {
                base.OnDrag(delta);
                if ((this.playerMove != null) && (base.enabled && base.gameObject.activeInHierarchy))
                {
                    if (this.playerControlSetInputFunc != null)
                    {
                        object[] args = new object[] { this.playerControl, this.normalizedPosition.x, this.normalizedPosition.y, false };
                        KLuaFunc.Call(this.playerControlSetInputFunc, args);
                    }
                    else
                    {
                        this.playerMove.Move(this.normalizedPosition.x, this.normalizedPosition.y);
                    }
                }
            }
    
            public void OnDrawGizmos()
            {
                Gizmos.color = Color.green;
                Vector3 to = base.transform.position + ((Vector3) (new Vector3(this.position.x, this.position.y, 0f) * 100f));
                Gizmos.DrawLine(base.transform.position, to);
            }
    
            public void SetPlayer(GameObject obj)
            {
                this.playerMove = obj.GetComponent<KMoveBehavior>();
            }
    
            public void SetPlayerControl(LuaTable control)
            {
                this.playerControl = control;
                this.playerControlSetInputFunc = KLuaBehavior.GetFunction(control, "SetJoyStickInput");
            }
    
            internal override void Start()
            {
                base.Start();
                this.parent = base.transform.parent;
                this.originPos = this.parent.position;
            }
        }
    }
  • 相关阅读:
    JSON特殊字符的处理
    java中高并发和高响应解决方法
    对redis深入理解
    对java中arraylist深入理解
    Redis的字典扩容与ConcurrentHashMap的扩容策略比较
    PHP压缩上传图片
    windows 平台 php_Imagick 拓展遇到的那些坑!
    windows7下php5.4成功安装imageMagick,及解决php imagick常见错误问题。(phpinfo中显示不出来是因为:1.imagick软件本身、php本身、php扩展三方版本要一致,2.需要把CORE_RL_*.dll多个文件放到/php/目录下面)
    php使用imagick模块实现图片缩放、裁剪、压缩示例
    文件打包,下载之使用PHP自带的ZipArchive压缩文件并下载打包好的文件
  • 原文地址:https://www.cnblogs.com/123ing/p/4065883.html
Copyright © 2011-2022 走看看