/// <summary> /// 获取波浪随机数的最大最小 /// </summary> /// <param name="str"></param> /// <returns></returns> public static FromToNumber ParseMinMaxNumber(string str) { var rangeArr = KTool.Split <float>(str, '~', '-'); var number = new FromToNumber(); if (rangeArr.Count > 0) { number.From = rangeArr[0]; } if (rangeArr.Count > 1) { number.To = rangeArr[1]; } return(number); }
protected static Transform GetParent(string bigType, string smallType) { var uri = GetUri(bigType, smallType); Transform theParent; if (!Parents.TryGetValue(uri, out theParent)) { var bigTypeObjName = string.Format("__{0}__", bigType); var bigTypeObj = GameObject.Find(bigTypeObjName) ?? new GameObject(bigTypeObjName); GameObject.DontDestroyOnLoad(bigTypeObj); bigTypeObj.transform.SetAsFirstSibling(); theParent = new GameObject(smallType).transform; KTool.SetChild(theParent, bigTypeObj.transform); Parents[uri] = theParent; } return(theParent); }
/// <summary> /// 设置某个物件,在指定调试组下 /// </summary> /// <param name="bigType"></param> /// <param name="smallType"></param> /// <param name="obj"></param> public static void SetParent(string bigType, string smallType, GameObject obj) { var uri = GetUri(bigType, smallType); Transform theParent = GetParent(bigType, smallType); int typeCount; if (!Counts.TryGetValue(uri, out typeCount)) { Counts[uri] = 0; } typeCount = ++Counts[uri]; try { KTool.SetChild(obj, theParent.gameObject); } catch (Exception e) { Log.Error(string.Format("[SetParent]{0}->{1}->{2}", bigType, smallType, e.Message)); } theParent.gameObject.name = GetNameWithCount(smallType, typeCount); }
private void Init() { if (AppConfig.IsLogDeviceInfo) { IsRootUser = KTool.HasWriteAccessToFolder(Application.dataPath); // Root User运行时,能穿越沙盒写DataPath, 以此为依据 Log.Info("===================================================================================="); Log.Info("Application.platform = {0}", Application.platform); Log.Info("Application.dataPath = {0} , WritePermission: {1}", Application.dataPath, IsRootUser); Log.Info("Application.streamingAssetsPath = {0} , WritePermission: {1}", Application.streamingAssetsPath, KTool.HasWriteAccessToFolder(Application.streamingAssetsPath)); Log.Info("Application.persistentDataPath = {0} , WritePermission: {1}", Application.persistentDataPath, KTool.HasWriteAccessToFolder(Application.persistentDataPath)); Log.Info("Application.temporaryCachePath = {0} , WritePermission: {1}", Application.temporaryCachePath, KTool.HasWriteAccessToFolder(Application.temporaryCachePath)); Log.Info("Application.unityVersion = {0}", Application.unityVersion); Log.Info("SystemInfo.deviceModel = {0}", SystemInfo.deviceModel); Log.Info("SystemInfo.deviceUniqueIdentifier = {0}", SystemInfo.deviceUniqueIdentifier); Log.Info("SystemInfo.graphicsDeviceVersion = {0}", SystemInfo.graphicsDeviceVersion); Log.Info("===================================================================================="); } StartCoroutine(DoInit()); }
/// <summary> /// 协和加载Assetbundle,加载完后执行callback /// </summary> /// <param name="url">资源的url</param> /// <param name="callback"></param> /// <param name="callbackArgs"></param> /// <returns></returns> private IEnumerator CoLoad(string url) { #if UNITY_2017_1_OR_NEWER //在Unity2017.1.1下,路径中包含两种分隔符(/和\),仅限windows平台 //比如:C:\Code\KSFramework\Product/Bundles/Windows/ui/login.prefab.k)会报: UriFormatException: Invalid URI: Invalid port number //此处对路径处理成Unity标准路径格式:C:/Code/KSFramework/Product/Bundles/Windows/ui/login.prefab.k #if UNITY_EDITOR || UNITY_STANDALONE_WIN url = KTool.FormatToAssetUrl(url); #endif #endif KResourceModule.LogRequest("WWW", url); System.DateTime beginTime = System.DateTime.Now; // 潜规则:不用LoadFromCache~它只能用在.assetBundle Www = new WWW(url); BeginLoadTime = Time.time; WWWLoadingCount++; //设置AssetBundle解压缩线程的优先级 Www.threadPriority = Application.backgroundLoadingPriority; // 取用全局的加载优先速度 while (!Www.isDone) { Progress = Www.progress; yield return(null); } yield return(Www); WWWLoadingCount--; Progress = 1; if (IsReadyDisposed) { Log.Error("[KWWWLoader]Too early release: {0}", url); OnFinish(null); yield break; } if (!string.IsNullOrEmpty(Www.error)) { if (Application.platform == RuntimePlatform.Android) { // TODO: Android下的错误可能是因为文件不存在! } string fileProtocol = KResourceModule.GetFileProtocol(); if (url.StartsWith(fileProtocol)) { string fileRealPath = url.Replace(fileProtocol, ""); Log.Error("File {0} Exist State: {1}", fileRealPath, System.IO.File.Exists(fileRealPath)); } Log.Error("[KWWWLoader:Error]{0} {1}", Www.error, url); OnFinish(null); yield break; } else { KResourceModule.LogLoadTime("WWW", url, beginTime); if (WWWFinishCallback != null) { WWWFinishCallback(url); } Desc = string.Format("{0}K", Www.bytes.Length / 1024f); OnFinish(Www); } // 预防WWW加载器永不反初始化, 造成内存泄露~ if (Application.isEditor) { while (GetCount <KWWWLoader>() > 0) { yield return(null); } yield return(new WaitForSeconds(5f)); while (Debug.isDebugBuild && !IsReadyDisposed) { Log.Error("[KWWWLoader]Not Disposed Yet! : {0}", this.Url); yield return(null); } } }
// 加载材质的图片, 协程等待 private IEnumerator CoGenerateMaterial(string matPath, KSerializeMaterial sMat) { // 纹理全部加载完成后到这里 //if (!CachedMaterials.TryGetValue(matPath, out mat)) { var shaderLoader = ShaderLoader.Load(sMat.ShaderPath); while (!shaderLoader.IsCompleted) { yield return(null); } var shader = shaderLoader.ShaderAsset; if (shader == null) { shader = KTool.FindShader(sMat.ShaderName); Log.Warning("无法加载Shader资源: {0}, 使用Shaders.Find代替", sMat.ShaderName); if (shader == null) { Log.Warning("找不到Shader: {0}, 使用Diffuse临时代替", sMat.ShaderName); shader = KTool.FindShader("Diffuse"); } } Debuger.Assert(shader); Mat = new Material(shader); Mat.name = sMat.MaterialName; //CachedMaterials[matPath] = mat; foreach (KSerializeMaterialProperty shaderProp in sMat.Props) { switch (shaderProp.Type) { case KSerializeMaterialProperty.ShaderType.Texture: Vector2 tiling; Vector2 offset; var texturePath = ParseMaterialStr(shaderProp.PropValue, out tiling, out offset); if (TextureLoaders == null) { TextureLoaders = new List <TextureLoader>(); } var texLoader = TextureLoader.Load(texturePath); TextureLoaders.Add(texLoader); while (!texLoader.IsCompleted) { yield return(null); } var tex = texLoader.Asset; if (tex == null) { Log.Error("找不到纹理: {0}", texturePath); } else { _SetMatTex(Mat, shaderProp.PropName, tex, tiling, offset); } break; case KSerializeMaterialProperty.ShaderType.Color: _SetMatColor(Mat, shaderProp.PropName, shaderProp.PropValue); break; case KSerializeMaterialProperty.ShaderType.Range: _SetMatRange(Mat, shaderProp.PropName, shaderProp.PropValue); break; case KSerializeMaterialProperty.ShaderType.Vector: _SetMatVector(Mat, shaderProp.PropName, shaderProp.PropValue); break; case KSerializeMaterialProperty.ShaderType.RenderTexture: // RenderTextures, 不处理, 一般用在水,Water脚本会自动生成 break; } } } }