Unity 3d中相关方法总结

来源:互联网 发布:python 爬虫 抓取json 编辑:程序博客网 时间:2024/04/30 00:50
    /// <summary>
    /// 计算NGUI中的物体在屏幕中所占位置
    /// </summary>
    /// <param name="go">NGUI中的物体</param>
    /// <returns>矩形区域</returns>
    private Rect NGUIObjectToRect(GameObject go)
    {
        Camera camera = NGUITools.FindCameraForLayer(go.layer);
        Bounds bounds = NGUIMath.CalculateAbsoluteWidgetBounds(go.transform);
        Vector3 min = camera.WorldToScreenPoint(bounds.min);
        Vector3 max = camera.WorldToScreenPoint(bounds.max);
        return new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
    }


    /// <summary>
    /// 判断NGUI中的物体物体是否超出屏幕边界
    /// </summary>
    /// <param name="go">对应NGUI物体</param>
    /// <returns>0:未超出边界,-1:超出左边界,1:超出右边界</returns>
    private int IsOverBoundary(GameObject go)
    {
        Camera camera = NGUITools.FindCameraForLayer(go.layer);
        Bounds bounds = NGUIMath.CalculateAbsoluteWidgetBounds(go.transform);
        Vector3 min = camera.WorldToScreenPoint(bounds.min);
        Vector3 max = camera.WorldToScreenPoint(bounds.max);
        if (min.x < 0)
        {
            return -1;
        }
        if (max.x > Screen.width )
        {
            return 1;
        }
        return 0;

    }


键盘按下的KEyCode的字符
public class getkey : MonoBehaviour {
        private string a="";
        void  OnGUI() {
 
                Event e  = Event.current;
 
                if (e.isKey) {
                        a=e.keyCode.ToString();
                }
                GUILayout.Label("按下键值为:"+a);
        }
}


    /// <summary>
    /// 判断鼠标是否在NGUI对应的GameObject中

    /// 此方法根据控件的BoxCollider判断,m_camera为UIcamera类型
    /// </summary>
    /// <param name="nGUIgo">NGUI对应的GameObject</param>
    /// <returns>bool</returns>
    private bool getMouseInNGUI(GameObject nGUIgo)
    {
        Vector2 goSize = nGUIgo.transform.GetComponent<BoxCollider>().size;
        Vector2 goPos = m_camera.cachedCamera.WorldToScreenPoint(nGUIgo.transform.position);
        Vector2 rectPos = new Vector2(goPos.x - goSize.x / 2, goPos.y + goSize.y / 2);
        Rect nguiGORect = new Rect(rectPos .x,rectPos .y,goSize .x,goSize .y);
        if (Input.mousePosition.x > nguiGORect.x && Input.mousePosition.x < nguiGORect.x + nguiGORect.width)
            if (Input.mousePosition.y < nguiGORect.y && Input.mousePosition.y > nguiGORect.y - nguiGORect.height)
                return true;
        return false;
    }

    /// <summary>
    /// 设置图片的相关尺寸
    /// 使之图片不被拉伸
    /// </summary>
    /// <param name="tf">Texture</param>
    private void SetTextureSize(Transform tf)
    {
        float rate;
        UITexture texture = tf.GetComponent<UITexture>();
        rate = texture.mainTexture.height * 1.0f / texture.mainTexture.width;


        tf.transform.localScale =new Vector3 ( tf.transform.localScale.y*1.0f / rate,tf.transform .localScale .y ,tf .transform .localScale .z );
    }


当人物移动与离开指定区域后,自动播放相关动画

  void OnTriggerEnter()

    {
        animation["AutoClose"].time = 0;
        animation["AutoClose"].speed =1.0f;
        animation.Play("AutoClose");
    }


    void OnTriggerExit()
    {
        animation["AutoClose"].time = animation["AutoClose"].clip.length;
        animation["AutoClose"].speed = -1.0f;
        animation.Play("AutoClose");

    } 


模型在UI中实现初始状态下360度自动旋转,手指拨动时,实现720度旋转

    #region 相关字段
    public EasyJoystick easyJoystick;
    private Vector2 mousePos;
    private GameObject modelObject;
    private float axisX;
    private float axisY;
    private float cXY;
    private float speed = 6f;
    private float tempSpeed;
    private bool m_isAutoRotate = true;
    #endregion
    #endregion

private void On_DragStart(Gesture gesture)
    {
        axisX = 0f;
        axisY = 0f;
        mousePos = gesture.position;
    }


    private void On_Drag(Gesture gesture)
    {
        if (gesture.position == mousePos)
        {
            m_isAutoRotate = false;
            return;
        }
        axisX = -Input.GetAxis("Mouse X");
        axisY = Input.GetAxis("Mouse Y");
        cXY = Mathf.Sqrt(axisX * axisX + axisY * axisY);
        if (cXY == 0f)
        {
            cXY = 1f;
        }
    }

    private void On_DragEnd(Gesture gesture)
    {
        m_isAutoRotate = true;
    }

   private float Rigid()
    {
        if (!m_isAutoRotate)
        {
            tempSpeed = speed;
        }
        else
        {
            if (tempSpeed > 0)
            {
                tempSpeed -= speed * 2 * Time.deltaTime / cXY;
            }
            else
            {
                tempSpeed = 0;
            }
        }
        return tempSpeed;
    }

    void Update()
    {


        if (modelObject == null)
        {
            Transform tran = transform.Find("MainPanel/EnlargeArea/Model");
            if (tran != null)
                modelObject = tran.gameObject;
        }
        if (modelObject != null && !m_isAutoRotate)
        {
            modelObject.transform.Rotate(new Vector3(axisY, axisX, 0) * Rigid(), Space.World);
        }
        if (modelObject != null && m_isAutoRotate)
        {
            modelObject.transform.Rotate(new Vector3(0, 1, 0), Space.World);
        }
    }


unity发不成web格式之后,原来的跳转都是覆盖原地址的,而这都不是我们想要的。看一下解决方法,利用网页的js来实现。 首先,在发布的网页里面加入一个函数。 
function linkApp(url){window.open(url,"blank");}然后,在unity3d里通Application.ExternalCall来调用这个函数。
if(Application.isWebPlayer){ 
 Application.ExternalCall("linkApp", "jianxiu/"+jianXiuURL[1]);
}else{
  Application.OpenURL("jianxiu\\"+jianXiuURL[1]);


场景加载进度

 void Start()

{

 StartCoroutine(loadScene());

}

IEnumerator loadScene()

{

async = Application.LoadLevelAsync(x);

yield return async;

}

void Update()

{

//progress 的取值范围在0.1 - 1之间, 但是它不会等于1//也就是说progress可能是0.9的时候就直接进入新场景了

progress = (int)(async.progress *100);

}


unity发布web,单击链接新建页面打开链接,利用网页的js来实现。

在发布的网页里面加入一个函数。 
function linkApp(url){window.open(url,"blank");}

然后,在unity3d里通Application.ExternalCall来调用这个函数。
if(Application.isWebPlayer)


       Application.ExternalCall("linkApp", "jianxiu/"+jianXiuURL[1]);

else

{
      Application.OpenURL("jianxiu\\"+jianXiuURL[1]);

0 0
原创粉丝点击