/*
********************************************************************************
*Copyright(C),coolae.net
*Author: chenbin
*Version: 2.0
*Date: 2017-01-09
*Description: 工具类
*Others:
*History:
*********************************************************************************
*/
using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Security.Cryptography;
using XLua;
#if !UNITY_WEBPLAYER
using System.Net.NetworkInformation;
#endif
namespace Coolape
{
public static class Utl
{
public static Vector3 kXAxis = new Vector3(1.0f, 0.0f, 0.0f);
public static Vector3 kZAxis = new Vector3(0.0f, 0.0f, 1.0f);
static string cacheUuid = "";
public static string uuid
{
get
{
if (string.IsNullOrEmpty(cacheUuid))
{
if (Application.platform == RuntimePlatform.Android)
{
cacheUuid = SystemInfo.deviceUniqueIdentifier;
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
#if UNITY_IOS || UNITY_IPHONE || UNITY_STANDALONE_OSX
string jsonStr = KeyChain.BindGetKeyChainUser ();
Debug.Log("jsonStr===========" + jsonStr);
if (string.IsNullOrEmpty (jsonStr)) {
cacheUuid = SystemInfo.deviceUniqueIdentifier;
KeyChain.BindSetKeyChainUser ("0", cacheUuid);
} else {
Hashtable m = JSON.DecodeMap(jsonStr);
cacheUuid = MapEx.getString(m, "uuid");
if(string.IsNullOrEmpty(cacheUuid)) {
cacheUuid = SystemInfo.deviceUniqueIdentifier;
KeyChain.BindSetKeyChainUser ("0", cacheUuid);
}
}
#endif
}
else
{
cacheUuid = GetMacAddress();
}
}
return cacheUuid;
}
}
public static string GetMacAddress()
{
#if !UNITY_WEBPLAYER
string macAdress = "";
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
PhysicalAddress address = adapter.GetPhysicalAddress();
if (address.ToString() != "")
{
macAdress = address.ToString();
return macAdress;
}
}
#endif
return "00";
}
public static Hashtable vector2ToMap(Vector2 v2)
{
Hashtable r = new Hashtable();
r["x"] = (double)(v2.x);
r["y"] = (double)(v2.y);
return r;
}
public static Hashtable vector3ToMap(Vector3 v3)
{
Hashtable r = new Hashtable();
r["x"] = (double)(v3.x);
r["y"] = (double)(v3.y);
r["z"] = (double)(v3.z);
return r;
}
public static Hashtable vector4ToMap(Vector4 v4)
{
Hashtable r = new Hashtable();
r["x"] = (double)(v4.x);
r["y"] = (double)(v4.y);
r["z"] = (double)(v4.z);
r["w"] = (double)(v4.w);
return r;
}
public static Vector2 mapToVector2(Hashtable map)
{
if (map == null)
{
return Vector2.zero;
}
return new Vector2(
(float)(MapEx.getDouble(map, "x")),
(float)(MapEx.getDouble(map, "y")));
}
public static Vector3 mapToVector3(Hashtable map)
{
if (map == null)
{
return Vector3.zero;
}
return new Vector3(
(float)(MapEx.getDouble(map, "x")),
(float)(MapEx.getDouble(map, "y")),
(float)(MapEx.getDouble(map, "z")));
}
public static Hashtable colorToMap(Color color)
{
Hashtable r = new Hashtable();
r["r"] = (double)(color.r);
r["g"] = (double)(color.g);
r["b"] = (double)(color.b);
r["a"] = (double)(color.a);
return r;
}
public static Color mapToColor(Hashtable map)
{
Color c = new Color(
(float)(MapEx.getDouble(map, "r")),
(float)(MapEx.getDouble(map, "g")),
(float)(MapEx.getDouble(map, "b")),
(float)(MapEx.getDouble(map, "a"))
);
return c;
}
///
/// Filters the path.过滤路径
///
///
/// The path.
///
///
/// Path.
///
public static string filterPath(string path)
{
string r = path;
if (path.IndexOf("Assets/") == 0)
{
r = StrEx.Mid(path, 7);
}
r = r.Replace("\\", "/");
r = r.Replace("/upgradeRes4Dev", "/upgradeRes");
r = r.Replace("/upgradeRes4Publish/", "/upgradeRes/");
return r;
}
///
/// Gets the animation curve.创建动画曲线
///
///
/// The animation curve.
///
///
/// List.
///
///
/// Post wrap mode.
///
///
/// Pre wrap mode.
///
public static AnimationCurve getAnimationCurve(ArrayList list, WrapMode postWrapMode, WrapMode preWrapMode)
{
if (list == null || list.Count <= 0)
{
return null;
}
int len = list.Count;
Keyframe[] ks = new Keyframe[len];
for (int i = 0; i < len; i++)
{
Hashtable m = (Hashtable)list[i];
float inTangent = (float)MapEx.getDouble(m, "inTangent");
float outTangent = (float)MapEx.getDouble(m, "outTangent");
float time = (float)MapEx.getDouble(m, "time");
float value = (float)MapEx.getDouble(m, "value");
ks[i] = new Keyframe(time, value, inTangent, outTangent);
}
AnimationCurve curve = new AnimationCurve(ks);
curve.preWrapMode = preWrapMode;
curve.postWrapMode = postWrapMode;
return curve;
}
///
/// Rotates the towards.转向目标方向(支持提前量)
///
///
/// Dir.
///
public static void rotateTowardsForecast(Transform trsf, Transform target, float forecastDis = 0)
{
Vector3 dir = Vector3.zero;
if (forecastDis > 0)
{
dir = target.position + target.forward * forecastDis - trsf.position;
}
else
{
dir = target.position - trsf.position;
}
RotateTowards(trsf, dir);
}
///
/// Rotates the towards.转向目标方向(立即)
///
///
/// Dir.
///
public static void RotateTowards(Transform trsf, Vector3 from, Vector3 to)
{
RotateTowards(trsf, to - from);
}
public static void RotateTowards(Transform trsf, Vector3 dir)
{
if (dir.magnitude < 0.001f)
{
return;
}
Quaternion rot = trsf.rotation;
Quaternion toTarget = Quaternion.LookRotation(dir);
trsf.rotation = toTarget;
}
///
/// Rotates the towards.转向目标方向(有转向过程)
///
///
/// Dir.
///
public static void RotateTowards(Transform transform, Vector3 dir, float turningSpeed)
{
try
{
Quaternion rot = transform.rotation;
if (dir.magnitude < 0.001f)
{
return;
}
Quaternion toTarget = Quaternion.LookRotation(dir);
rot = Quaternion.Slerp(rot, toTarget, turningSpeed * Time.fixedDeltaTime);
Vector3 euler = rot.eulerAngles;
//euler.z = 0;
//euler.x = 0;
rot = Quaternion.Euler(euler);
transform.rotation = rot;
}
catch (System.Exception e)
{
Debug.Log("name==" + transform.name + " " + e);
}
}
public static Vector3 getAngle(Transform tr, Vector3 pos2)
{
return getAngle(tr.position, pos2);
}
public static Vector3 getAngle(Vector3 pos1, Vector3 pos2)
{
Vector3 dir = pos2 - pos1;
return getAngle(dir);
}
public static Vector3 getAngle(Vector3 dir)
{
if (dir.magnitude < 0.001f)
{
return Vector3.zero;
}
Quaternion toTarget = Quaternion.LookRotation(dir);
Vector3 euler = toTarget.eulerAngles;
return euler;
// return Quaternion.Euler(euler).eulerAngles;
}
///
/// Sets the body mat edit.重新设置一次shader,在editor模式下加载assetsbundle才需要调用这个方法
///
///
/// Tr.
///
public static void setBodyMatEdit(Transform tr)
{
setBodyMatEdit(tr, null);
}
public static void setBodyMatEdit(Transform tr, Shader defaultShader)
{
if (tr == null)
{
return;
}
string shName = "";
if (tr.GetComponent() != null && tr.GetComponent().sharedMaterial != null)
{
shName = tr.GetComponent().sharedMaterial.shader.name;
if (defaultShader != null)
{
tr.GetComponent().sharedMaterial.shader = defaultShader;
}
else
{
tr.GetComponent().sharedMaterial.shader = Shader.Find(shName);
}
}
SkinnedMeshRenderer smr = tr.GetComponent();
if (smr != null)
{
shName = smr.sharedMaterial.shader.name;
if (defaultShader != null)
{
smr.sharedMaterial.shader = defaultShader;
}
else
{
smr.sharedMaterial.shader = Shader.Find(shName);
}
}
MeshRenderer mr = tr.GetComponent();
if (mr != null)
{
shName = mr.sharedMaterial.shader.name;
if (defaultShader != null)
{
mr.sharedMaterial.shader = defaultShader;
}
else
{
mr.sharedMaterial.shader = Shader.Find(shName);
}
foreach (Material m in mr.sharedMaterials)
{
shName = m.shader.name;
if (defaultShader != null)
{
m.shader = defaultShader;
}
else
{
m.shader = Shader.Find(shName);
}
}
}
TrailRenderer tailRender = tr.GetComponent();
if (tailRender != null)
{
shName = tailRender.sharedMaterial.shader.name;
if (defaultShader != null)
{
tailRender.sharedMaterial.shader = defaultShader;
}
else
{
tailRender.sharedMaterial.shader = Shader.Find(shName);
}
}
for (int i = 0; i < tr.childCount; i++)
{
setBodyMatEdit(tr.GetChild(i));
}
}
public static float distance(Transform tr1, Transform tr2)
{
return Vector3.Distance(tr1.position, tr2.position);
}
public static float distance4Loc(Transform tr1, Transform tr2)
{
return Vector3.Distance(tr1.localPosition, tr2.localPosition);
}
public static float distance(Vector2 v1, Vector2 v2)
{
return Vector2.Distance(v1, v2);
}
public static float distance(Vector3 v1, Vector3 v2)
{
return Vector3.Distance(v1, v2);
}
public static string LuaTableToString(LuaTable map)
{
if (map == null)
return "map is null";
StringBuilder outstr = new StringBuilder();
LuaTableToString(map, outstr);
return outstr.ToString();
}
public static void LuaTableToString(LuaTable map, StringBuilder outstr, int spacecount = 0)
{
// IEnumerable