API自学记录(二)

API自学记录(二)

跟鼠标相关事件函数讲解

OnMouseDown鼠标按下时调用 OnMouseUp鼠标抬起时调用
OnMouseDrag按下过程中进行拖拽
OnMouseEnter鼠标移上去时
OnMouseExit鼠标移出去时
OnMouseOver鼠标在上面时
OnMouseUpAsButton在鼠标抬起时触发(必须在同一游戏物体身上)

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

public class OnMouseEvent09 : MonoBehaviour {


    void OnMouseDown()
    {
        print("Down" + gameObject);
    }
    void OnMouseUp()
    {
        print("up" + gameObject);
    }
    void OnMouseDrag()
    {
        print("Drag" + gameObject);
    }

    void OnMouseEnter()
    {
        print("Enter");
    }
    void OnMouseExit()
    {
        print("Exit");
    }
    void OnMouseOver()
    {
        print("Over");
    }

    void OnMouseUpAsButton()
    {
        print("Button" + gameObject);
    }
}

在这里插入图片描述

Mathf里面的静态常量

PI=Π
Deg2Rad由度数变为弧度
Epsilon一个非常小的小数
Infinity无限大的整数

public class Mathf10 : MonoBehaviour {
	// Use this for initialization
	void Start () {
        print(Mathf.Deg2Rad);
        print(Mathf.Rad2Deg);
        print(Mathf.Infinity);
        print(Mathf.NegativeInfinity);
        print(Mathf.PI);
        print(Mathf.Epsilon);

    }

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

在这里插入图片描述

Mathf中的Clamp限定方法:

Ceil向上取整 CeilToInt
Abs取绝对值
Clamp最小值与最大值之间返回value,小于最小值取最小值,大于最大值取最大值

public class Mathf10 : MonoBehaviour {
    public Transform cube;
	// Use this for initialization
	void Start () {
    }
    // Update is called once per frame
    void Update () {
        //cube.position = new Vector3(Mathf.Clamp(Time.time, 1.0F, 3.0F), 0, 0);
        //Debug.Log(Mathf.Clamp(Time.time, 1.0F, 3.0F));
    }
    private int hp = 100;
    void TakeDamage()
    {
        hp -= 9;

        //if (hp < 0)
        //    hp = 0;
        hp = Mathf.Clamp(hp, 0, 100);
    }
}

在这里插入图片描述

Mathf中常用的方法

DeltaAngle计算最短距离(最小差)
Floor向下取整
Pow(f,p)取得f的p次方
Sqrt求参数的平方根
测试:

    print(Mathf.ClosestPowerOfTwo(2));//2
    print(Mathf.ClosestPowerOfTwo(3));//4
    print(Mathf.ClosestPowerOfTwo(4));//4
    print(Mathf.ClosestPowerOfTwo(5));//4
    print(Mathf.ClosestPowerOfTwo(6));//8
    print(Mathf.ClosestPowerOfTwo(30));//32
    print(Mathf.Max(1, 2));//2
    print(Mathf.Max(1, 2, 5, 3, 10));//10
    print(Mathf.Min(1, 2));//1
    print(Mathf.Min(1, 2, 5, 3, 10));//1
    print(Mathf.Pow(4, 3));//64 
    print(Mathf.Sqrt(3));//1.6左右

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值