public class API07Invoke : MonoBehaviour {
// Use this for initialization
void Start () {
//Invoke("Attack",3);
InvokeRepeating("Attack", 4, 2);
CancelInvoke("Attack");
}
void Update()
{
bool res = IsInvoking("Attack");
print(res);
}
void Attack()
{
print("攻击目标");
}
跟鼠标相关事件函数OnMouseXXX讲解
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ExampleClass : MonoBehaviour
{
void OnMouseDown()
{
// Destroy the gameObject after clicking on it
Destroy(gameObject);
}
}
022-Mathf里面的静态常量
Static Properties
Deg2Rad Degrees-to-radians conversion constant (Read Only).
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public float deg = 30.0F;
void Start()
{
float rad = deg * Mathf.Deg2Rad;
Debug.Log(deg + " degrees are equal to " + rad + " radians.");
}
}
Epsilon A tiny floating point value (Read Only).
using UnityEngine;
public class Example : MonoBehaviour
{
// Compares two floating point numbers and return true if they are the same number.
// See also Mathf.Approximately, which compares floating point numbers so you dont have
// to create a function to compare them.
bool isEqual(float a, float b)
{
if (a >= b - Mathf.Epsilon && a <= b + Mathf.Epsilon)
{
return true;
}
else
{
return false;
}
}
}
Infinity A representation of positive infinity (Read Only).
NegativeInfinity A representation of negative infinity (Read Only).
PI The well-known 3.14159265358979… value (Read Only).
Rad2Deg Radians-to-degrees conversion constant (Read Only).
023-Mathf中的Clamp限定方法
using UnityEngine;
// Mathf.Clamp example.
//
// Animate a cube along the x-axis using a sine wave.
// Let the minimum and maximum positions on the x-axis
// be changed. The cube will be visible inside the
// minimum and maximum values.
public class ExampleScript : MonoBehaviour
{
private float xMin = -1.0f, xMax = 1.0f;
private float timeValue = 0.0f;
void Update()
{
// Compute the sin position.
float xValue = Mathf.Sin(timeValue * 5.0f);
// Now compute the Clamp value.
float xPos = Mathf.Clamp(xValue, xMin, xMax);
// Update the position of the cube.
transform.position = new Vector3(xPos, 0.0f, 0.0f);
// Increase animation time.
timeValue = timeValue + Time.deltaTime;
// Reset the animation time if it is greater than the planned time.
if (xValue > Mathf.PI * 2.0f)
{
timeValue = 0.0f;
}
}
void OnGUI()
{
// Let the minimum and maximum values be changed
xMin = GUI.HorizontalSlider(new Rect(25, 25, 100, 30), xMin, -1.0f, +1.0f);
xMax = GUI.HorizontalSlider(new Rect(25, 60, 100, 30), xMax, -1.0f, +1.0f);
// xMin is kept less-than or equal to xMax.
if (xMin > xMax)
{
xMin = xMax;
}
// Display the xMin and xMax value with better size labels.
GUIStyle fontSize = new GUIStyle(GUI.skin.GetStyle(“label”));
fontSize.fontSize = 24;
GUI.Label(new Rect(135, 10, 150, 30), "xMin: " + xMin.ToString(“f2”), fontSize);
GUI.Label(new Rect(135, 45, 150, 30), "xMax: " + xMax.ToString(“f2”), fontSize);
}
}
API10Mathf
public class API10Mathf : MonoBehaviour {
public Transform cube;
public int a = 8;
public int b = 20;
public float t = 0;
public float speed = 3;
// Use this for initialization
void Start()
{
//print(Mathf.Deg2Rad);
//print(Mathf.Rad2Deg);
//print(Mathf.Infinity);
//print(Mathf.NegativeInfinity);
//print(Mathf.PI);
//print(Mathf.Epsilon);
//Debug.Log(Mathf.Floor(10.0F));
//Debug.Log(Mathf.Floor(10.2F));
//Debug.Log(Mathf.Floor(10.7F));
//Debug.Log(Mathf.Floor(-10.0F));
//Debug.Log(Mathf.Floor(-10.2F));
//Debug.Log(Mathf.Floor(-10.7F));
// 2 4 8 16 32
//print(Mathf.ClosestPowerOfTwo(2));//4
//print(Mathf.ClosestPowerOfTwo(3));//8
//print(Mathf.ClosestPowerOfTwo(4));//8
//print(Mathf.ClosestPowerOfTwo(5));//8
//print(Mathf.ClosestPowerOfTwo(6));//8
//print(Mathf.ClosestPowerOfTwo(30));//8
//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
cube.position = new Vector3(0, 0, 0);
}
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));
//print(Mathf.Lerp(a, b, t));
//float x = cube.position.x;
float newX = Mathf.Lerp(x, 10, Time.deltaTime);
//float newX = Mathf.MoveTowards(x, 10, Time.deltaTime*speed);
//cube.position = new Vector3(newX, 0, 0);
//print(Mathf.MoveTowards(a, b, t));
//print(Mathf.PingPong(t, 20));
cube.position = new Vector3(5+Mathf.PingPong(Time.time*speed, 5), 0, 0);
}
private int hp = 100;
void TakeDamage()
{
hp -= 9;
//if (hp < 0)
// hp = 0;
hp = Mathf.Clamp(hp,0, 100);
}
}