123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- using System;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class NumberUtil
- {
- public static float angleToRadian(float angle)
- {
- return angle/180*Mathf.PI;
- }
-
- public static float radianToAngle(float radian)
- {
- return radian/Mathf.PI*180;
- }
- public static float getRadianByATan(float targetX, float targetY, float originX, float originY)
- {
- float dx = targetX-originX;
- float dy = targetY-originY;
- return Mathf.Atan2(dy, dx);
- }
- public static float forceBetween(float origin, float min, float max)
- {
- if(origin < min) return min;
- if(origin > max) return max;
- return origin;
- }
- public static int forceBetween(int origin, int min, int max)
- {
- if(origin < min) return min;
- if(origin > max) return max;
- return origin;
- }
-
- public static float getCloseToTargetAngle(float origin, float target, float step)
- {
- origin = corverAngleBetween(origin, -180f, 180f);
- target = corverAngleBetween(target, -180f, 180f);
- float deltaAngle = Mathf.Abs(target - origin);
- if(deltaAngle<=step || deltaAngle >= (360f-step))
- {
- origin = target;
- }
- else if(target > origin)
- {
- if(target-origin > 180f)
- {
- origin -= step;
- }
- else
- {
- origin += step;
- }
- }
- else
- {
- if(origin-target > 180f)
- {
- origin += step;
- }
- else
- {
- origin -= step;
- }
- }
- return origin;
- }
- public static float corverAngleBetween(float angle, float min, float max)
- {
- if(angle < min)
- {
- while(angle < min)
- {
- angle += 360f;
- }
- }
- else if(angle > max)
- {
- while(angle > max)
- {
- angle -= 360f;
- }
- }
- return angle;
- }
- public static float distanceVector2(Vector2 origin, Vector2 target)
- {
- return Vector2.Distance(origin, target);
- }
- public static float distanceVector3(Vector3 origin, Vector3 target, bool ignoreY=false)
- {
- if(ignoreY)
- origin.y = target.y = 0;
- return Vector3.Distance(origin, target);
- }
- public static string getTimeString(float seconds, bool enableHour=true)
- {
- bool isNegative = false;
- if(seconds < 0)
- {
- isNegative = true;
- seconds = -seconds;
- }
- int sec = (int)seconds;
- int h = enableHour?sec/(60*60):0;
- int tempSec = enableHour?sec%(60*60):sec;
- int m = tempSec/60;
- tempSec = tempSec%60;
- int s = tempSec;
- string hStr = enableHour?("00"+h):"";
- string mStr = "00"+m;
- string sStr = "00"+s;
- string str = "";
- if(enableHour)
- str = hStr.Substring(hStr.Length-2, 2)+":"+mStr.Substring(mStr.Length-2, 2)+":"+sStr.Substring(sStr.Length-2, 2);
- else
- str = mStr.Substring(mStr.Length-2, 2)+":"+sStr.Substring(sStr.Length-2, 2);
- if(isNegative)
- str = "-"+str;
- return str;
- }
- public static string GetPercentText(double value, int digits)
- {
- string str = Math.Round((double)(value * 100), digits).ToString();
- return str + "%";
- }
- public static string AddThousandSplit(int value)
- {
- List<string> list = new List<string>();
- do{
- int num = value%1000;
- value = value/1000;
- if(value > 0)
- list.Insert(0, StringUtil.FillZero(num, 3));
- else
- list.Insert(0, num.ToString());
- }
- while(value != 0);
- return StringUtil.Join(list, " ");
- }
- }
|