/// <summary> /// Saves the script. /// </summary> /// <returns><c>true</c>, if script was saved, <c>false</c> otherwise.</returns> /// <param name="path">Path.</param> /// <param name="subFix">Sub fix.</param> /// <param name="code">Code.</param> /// <param name="overwrite">If set to <c>true</c> overwrite.</param> /// <param name="currentBaseType">Current base type.</param> /// <param name="newBaseType">New base type.</param> static public bool SaveScript(string path, string code, bool overwrite, string currentBaseType = "", string newBaseType = "") { try { if (!overwrite && File.Exists(path)) { return(false); } string currentCode = ""; if (File.Exists(path)) { currentCode = File.ReadAllText(path); } if (!code.Equals(currentCode)) { File.WriteAllText(path, code, System.Text.Encoding.UTF8); return(true); } else { return(false); } } catch (IOException ex) { UnuLogger.LogError(ex); return(false); } }
public void TriggerEvent(string eventType) { Delegate @delegate; if (Routers.TryGetValue(eventType, out @delegate)) { Delegate[] invocationList = @delegate.GetInvocationList(); for (int i = 0; i < invocationList.Length; i++) { Action action = invocationList [i] as Action; if (action == null) { throw new EventException(string.Format("TriggerEvent {0} error: types of parameters are not match.", eventType)); } try { action(); } catch (Exception ex) { UnuLogger.LogError(ex.StackTrace); } } } }
/// <summary> /// Saves the script. /// </summary> /// <returns><c>true</c>, if script was saved, <c>false</c> otherwise.</returns> /// <param name="path">Path.</param> /// <param name="subFix">Sub fix.</param> /// <param name="code">Code.</param> /// <param name="overwrite">If set to <c>true</c> overwrite.</param> /// <param name="currentBaseType">Current base type.</param> /// <param name="newBaseType">New base type.</param> public static bool SaveScript(string path, string code, bool overwrite, string currentBaseType = "", string newBaseType = "") { try { if (!overwrite && File.Exists(path)) { return(false); } var currentCode = ""; if (File.Exists(path)) { currentCode = AssetDatabase.LoadAssetAtPath <TextAsset>(path).text; } if (!code.Equals(currentCode)) { using (var writer = new StreamWriter(path)) { writer.Write(code); } return(true); } else { return(false); } } catch (IOException ex) { UnuLogger.LogException(ex); return(false); } }
static public object GetDefaultValue(Type type) { try { return(Activator.CreateInstance(type)); } catch { UnuLogger.LogError("Cannot get default value of target type!"); return(null); } }
public T GetLastValueAs <T> () { try { return((T)LastValue); } catch { UnuLogger.Log("Type of property has been changed, the default value will set to default value of new type!"); return(default(T)); } }
// Update is called once per frame private void Update() { if (!this.isRunning) { return; } var deltaTime = Time.realtimeSinceStartup - this.lastTime; this.lastTime = Time.realtimeSinceStartup; if (this.time > 0) { this.t += deltaTime / this.time; } else { this.t = 1; } switch (this.tweenType) { case UITweenType.Value: case UITweenType.Alpha: this.currentValue = Mathf.Lerp(this.startValue, this.endValue, this.t); if (this.tweenType == UITweenType.Alpha) { if (this.canvasGroup != null) { this.canvasGroup.alpha = this.currentValue; } else if (this.image != null) { this.image.color = new Color(this.image.color.r, this.image.color.g, this.image.color.b, this.currentValue); } else { UnuLogger.LogWarning(this.gameObject.name + " have no CanvasGroup or Image. TweenAlpha require component that contain alpha value!"); } } break; case UITweenType.Move: this.transform.localPosition = Vector3.Lerp(this.originalPosition, this.targetPosition, this.t); break; } if (this.t >= 1f) { this.isRunning = false; this.onUpdate?.Invoke(this.currentValue); this.onComplete?.Invoke(); } else { this.onUpdate?.Invoke(this.currentValue); } }
public void OnUpdateColor(object newColor) { if (newColor == null) { return; } try { image.color = (Color)newColor; } catch { UnuLogger.LogWarning("Binding field is not a color!"); } }
/// <summary> /// Hides the screen. /// </summary> /// <param name="content">Content.</param> public void HideScreen(Type uiType) { if (this.screenDict.TryGetValue(uiType, out UIManScreen screen)) { screen.OnHide(); OnHideUI(screen); DoAnimHide(screen); } else { UnuLogger.LogWarningFormat(this, $"There are no UI of {uiType.Name} has been show!"); } }
/// <summary> /// Notify the property which has change to all binder that has been subcribed with property name and value. /// </summary> /// <param name="propertyName"></param> /// <param name="value"></param> public virtual void NotifyPropertyChanged(string propertyName, object value) { Action <object> actions = null; UnityEngine.Debug.LogError("change " + propertyName); if (actionDict.TryGetValue(propertyName, out actions)) { actions(value); } else { UnuLogger.LogWarning(BindingDefine.NO_BINDER_REGISTERED); } }
/// <summary> /// Hides the dialog. /// </summary> /// <param name="content">Content.</param> public void HideDialog(Type uiType) { if (IsInDialogTransition) { EnqueueDialog(uiType, UITransitionType.HIDE, null, null); return; } UIManDialog dialog = null; if (dialogDict.TryGetValue(uiType, out dialog)) { if (dialog.State == UIState.HIDE) { return; } if (activeDialog.Count > 0) { activeDialog.Pop(); } BringToLayer(dialogRoot, cover, cover.GetSiblingIndex() - 1); BringToLayer(dialogRoot, dialog.transform, cover.GetSiblingIndex() - 1); UIManDialog prevDialog = null; if (activeDialog.Count > 0) { dialogDict.TryGetValue(activeDialog.Peek(), out prevDialog); } if (prevDialog != null && prevDialog.useCover) { cover.gameObject.SetActive(true); } else { cover.gameObject.SetActive(false); } IsInDialogTransition = true; dialog.OnHide(); OnHideUI(dialog); DoAnimHide(dialog); } else { UnuLogger.LogFormatWarning("There are no UI of {0} has been show!", uiType.Name); return; } }
/// <summary> /// Hides the screen. /// </summary> /// <param name="content">Content.</param> public void HideScreen(Type uiType) { UIManScreen screen = null; if (screenDict.TryGetValue(uiType, out screen)) { screen.OnHide(); OnHideUI(screen); DoAnimHide(screen); } else { UnuLogger.LogFormatWarning("There are no UI of {0} has been show!", uiType.Name); return; } }
/// <summary> /// Notify the property which has change to all binder that has been subcribed with property name and value. /// </summary> /// <param name="propertyName"></param> /// <param name="value"></param> public virtual void NotifyPropertyChanged(string propertyName, object value) { if (!this.actions.TryGetValue(propertyName, out Action <object> actions)) { return; } try { actions?.Invoke(value); } catch (Exception e) { UnuLogger.LogException(e, this); } }
/// <summary> /// Backs the screen. /// </summary> /// <param name="args">Arguments.</param> public void BackScreen(params object[] args) { if (this.screenQueue.Count <= 1) { UnuLogger.LogWarning("UI Error: There are no scene has been loaded before this scene!", this); return; } this.CurrentScreen.HideMe(); UIManScreen beforeScreen = this.screenQueue[this.screenQueue.Count - 2]; OnBack(this.CurrentScreen, beforeScreen, args); this.screenQueue.RemoveAt(this.screenQueue.Count - 1); ShowScreen(beforeScreen.UIType, true, args); }
static void EditLoading() { GameObject obj = Resources.Load("UIActivityIndicator") as GameObject; if (obj != null) { string editorPath = AssetDatabase.GetAssetPath(obj); GameObject prefab = PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath <GameObject> (editorPath)) as GameObject; prefab.transform.SetParent(UIMan.Instance.transform, false); prefab.transform.SetAsLastSibling(); Selection.activeObject = prefab; } else { UnuLogger.LogError("ActivityIndicator prefab could not be found! Please re-install UIMan!"); } }
/// <summary> /// Raise the change event automatically, /// only use this function in property getter /// </summary> public void OnPropertyChanged(string propertyName, object value) { this.PropertyChanged?.Invoke(this); if (this.actions.TryGetValue($"set_{propertyName}", out Action <object> actions)) { try { actions?.Invoke(value); } catch (Exception e) { UnuLogger.LogException(e); } } DataContext.NotifyObjectChange(this, propertyName, value); }
static public string GenerateViewModelHandler(string modelName, string viewModelType) { string code = ""; TextAsset text = AssetDatabase.LoadAssetAtPath <TextAsset> (Getpath(VIEW_MODEL_HANDLER_PATH)); if (text != null) { code = text.text; code = Regex.Replace(code, NAME_TAG, modelName); code = Regex.Replace(code, TYPE_TAG, viewModelType); } else { UnuLogger.LogError("There are something wrong, could not find code template!"); } return(code); }
static public string DeleteScript(string path) { string code = ""; if (!File.Exists(path)) { return(null); } try { code = AssetDatabase.LoadAssetAtPath <TextAsset>(path).text; AssetDatabase.DeleteAsset(path); } catch (IOException ex) { UnuLogger.LogError(ex); } return(code); }
public static string GenerateHandler(string modelName, string baseType, UIManConfig config, string customNamespace = null) { var code = ""; var text = AssetDatabase.LoadAssetAtPath <TextAsset>(Getpath(HANDLER_TEMPLATE_PATH)); if (text != null) { code = text.text; code = Regex.Replace(code, NAME_SPACE_TAG, string.IsNullOrEmpty(customNamespace) ? GetNamespace(config) : customNamespace); code = Regex.Replace(code, NAME_TAG, modelName); code = Regex.Replace(code, TYPE_TAG, baseType); } else { UnuLogger.LogError("There are something wrong, could not find code template!"); } return(code); }
/// <summary> /// Notify the property which has change to all binder that has been subcribed with property name and value. /// </summary> /// <param name="propertyName"></param> /// <param name="value"></param> public virtual void NotifyPropertyChanged(string propertyName, object value) { Action <object> actions = null; if (actionDict.TryGetValue(propertyName, out actions)) { try { actions(value); } catch (Exception e) { UnuLogger.LogError(e.Message); } } else { //UnuLogger.LogWarning(BindingDefine.NO_BINDER_REGISTERED); } }
/// <summary> /// Dos the animation idle. /// </summary> /// <param name="ui">User interface.</param> public void DoAnimIdle(UIManBase ui) { if (ui.motionIdle == UIMotion.CUSTOM_MECANIM_ANIMATION) //Custom animation use animator { ui.animRoot.EnableAndPlay(UIManDefine.ANIM_IDLE); } else if (ui.motionHide == UIMotion.CUSTOM_SCRIPT_ANIMATION) //Custom animation use overrided function { ui.animRoot.Disable(); StartCoroutine(DelayDequeueDialog(ui.AnimationIdle(), ui, false)); } else // Simple tween { ui.animRoot.Disable(); if (ui.motionIdle != UIMotion.NONE && ui.motionIdle != UIMotion.HIDDEN) { UnuLogger.LogWarning("UIMan does not support simple tween animation for idle yet!"); } } }
static public string GenerateScript(string modelName, string baseType, params CustomPropertyInfo[] properties) { string code = ""; TextAsset text = AssetDatabase.LoadAssetAtPath <TextAsset> (Getpath(TYPE_PATH)); if (text != null) { code = text.text; code = Regex.Replace(code, NAME_TAG, modelName); code = Regex.Replace(code, TYPE_TAG, baseType); code = Regex.Replace(code, PROPERTIES_TAG, GeneratePropertiesBlock(properties)); code = Regex.Replace(code, NAME_SPACES_TAG, GenerateNameSpaceBlock(properties)); } else { UnuLogger.LogError("There are something wrong, could not find code template!"); } return(code); }
public static T GetPrefab <T>(bool autoSelection = true) where T : Object { var typeName = typeof(T).Name; var guids = AssetDatabase.FindAssets($"{typeName} t:{nameof(GameObject)}"); if (guids == null || guids.Length <= 0) { UnuLogger.LogError($"Cannot find any prefab of {typeName}."); return(null); } var file = AssetDatabase.GUIDToAssetPath(guids[0]); var prefab = AssetDatabase.LoadAssetAtPath <T>(file); if (autoSelection) { Selection.activeObject = prefab; } return(prefab); }
/// <summary> /// Preprocesses the UI. /// </summary> /// <param name="prefab">Prefab.</param> /// <param name="args">Arguments.</param> void PreprocessUI(GameObject prefab, object[] args) { Type uiType = (Type)args [0]; if (prefab == null) { UnuLogger.LogFormatWarning("UI Error: cannot find {0}, make sure you have put UI prefab in Resources folder!", uiType.Name); return; } GameObject uiObj = Instantiate(prefab) as GameObject; uiObj.name = uiType.Name; UIManBase uiBase = uiObj.GetComponent <UIManBase> (); if (uiBase is UIManScreen) { uiBase.Trans.SetParent(screenRoot, false); uiBase.RectTrans.localScale = Vector3.one; if (!screenDict.ContainsKey(uiType)) { screenDict.Add(uiType, uiBase as UIManScreen); } bool seal = (bool)args [1]; object[] param = (object[])args [2]; ShowScreen(uiType, seal, param); } else if (uiBase is UIManDialog) { uiBase.Trans.SetParent(dialogRoot, false); uiBase.RectTrans.localScale = Vector3.one; dialogDict.Add(uiType, uiBase as UIManDialog); UICallback callbacks = (UICallback)args [1]; object[] param = (object[])args [2]; IsLoadingDialog = false; ShowDialog(uiType, callbacks, param); } }
public void TriggerEvent(string eventType) { if (this.Routers.TryGetValue(eventType, out Delegate @delegate)) { Delegate[] invocationList = @delegate.GetInvocationList(); for (var i = 0; i < invocationList.Length; i++) { if (!(invocationList[i] is Action action)) { throw new EventException(string.Format("TriggerEvent {0} error: types of parameters are not match.", eventType)); } try { action(); } catch (Exception ex) { UnuLogger.LogException(ex); } } } }
public static OptionData Convert(object value, bool forceToString, Object context) { if (value == null) { return(_defaultValue); } if (!(value is OptionData val)) { if (forceToString) { val = new OptionData(value.ToString()); } else { UnuLogger.LogError($"Cannot convert '{value}' to {nameof(TMP_Dropdown)}.{nameof(OptionData)}.", context); val = _defaultValue; } } return(val); }
public static string GenerateType(string modelName, string baseType, bool isSealed, UIManConfig config, string customNamespace, params CustomPropertyInfo[] properties) { var code = ""; var text = AssetDatabase.LoadAssetAtPath <TextAsset>(Getpath(TYPE_TEMPLATE_PATH)); if (text != null) { code = text.text; code = Regex.Replace(code, NAME_SPACE_TAG, string.IsNullOrEmpty(customNamespace) ? GetNamespace(config) : customNamespace); code = Regex.Replace(code, NAME_TAG, modelName); code = Regex.Replace(code, TYPE_TAG, baseType); code = Regex.Replace(code, SEALED_TAG, isSealed ? "sealed " : string.Empty); code = Regex.Replace(code, PROPERTIES_TAG, GeneratePropertiesBlock(properties)); code = Regex.Replace(code, NAME_SPACES_TAG, GenerateNamespaceBlock(properties)); } else { UnuLogger.LogError("There are something wrong, could not find code template!"); } return(code); }
void PreprocessPreload(GameObject prefab, object[] args) { Type uiType = (Type)args [0]; if (prefab == null) { UnuLogger.LogFormatWarning("UI Error: cannot find {0}, make sure you have put UI prefab in Resources folder!", uiType.Name); return; } GameObject uiObj = Instantiate(prefab) as GameObject; uiObj.name = uiType.Name; uiObj.GetComponent <CanvasGroup> ().alpha = 0; UIManBase uiBase = uiObj.GetComponent <UIManBase> (); if (uiBase is UIManScreen) { uiBase.Trans.SetParent(screenRoot, false); uiBase.RectTrans.localScale = Vector3.one; if (!screenDict.ContainsKey(uiType)) { screenDict.Add(uiType, uiBase as UIManScreen); } } else if (uiBase is UIManDialog) { uiBase.Trans.SetParent(dialogRoot, false); uiBase.RectTrans.localScale = Vector3.one; if (!dialogDict.ContainsKey(uiType)) { dialogDict.Add(uiType, uiBase as UIManDialog); } } uiBase.ForceState(UIState.HIDE); }
/// <summary> /// Dos the animation idle. /// </summary> /// <param name="ui">User interface.</param> public void DoAnimIdle(UIManBase ui) { if (ui.motionIdle == UIMotion.CustomMecanimAnimation) { // Custom animation use animator ui.animRoot.EnableAndPlay(UIManDefine.ANIM_IDLE); } else if (ui.motionHide == UIMotion.CustomScriptAnimation) { // Custom animation use overrided function ui.animRoot.Disable(); StartCoroutine(DelayDequeueDialog(ui.AnimationIdle(), ui, false)); } else { // Simple tween ui.animRoot.Disable(); if (ui.motionIdle != UIMotion.None && ui.motionIdle != UIMotion.Hidden) { UnuLogger.LogWarning("UIMan does not support simple tween animation for idle yet!", this); } } }
// Update is called once per frame void Update() { if (!isRunning) { return; } float deltaTime = Time.realtimeSinceStartup - lastTime; lastTime = Time.realtimeSinceStartup; if (time > 0) { t += deltaTime / time; } else { t = 1; } switch (tweenType) { case UITweenType.VALUE: case UITweenType.ALPHA: currentValue = Mathf.Lerp(startValue, endValue, t); if (tweenType == UITweenType.ALPHA) { if (canvasGroup != null) { canvasGroup.alpha = currentValue; } else if (image != null) { image.color = new Color(image.color.r, image.color.g, image.color.b, currentValue); } else { UnuLogger.LogWarning(gameObject.name + " have no CanvasGroup or Image. TweenAlpha require component that contain alpha value!"); } } break; case UITweenType.MOVE: transform.localPosition = Vector3.Lerp(originalPosition, targetPosition, t); break; } if (t >= 1f) { isRunning = false; if (onUpdate != null) { onUpdate(currentValue); } if (onComplete != null) { onComplete(); } } else { if (onUpdate != null) { onUpdate(currentValue); } } }
public void GenerateType() { if (this.typeName.Contains(" ")) { EditorUtility.DisplayDialog("Error", "Class name cannot constain special characters", "OK"); return; } var warn = false; if (this.typeName.Length <= 1 || (!this.typeName.Substring(0, 2).Equals("UI") && !this.baseTypePopup.SelectedItem.Equals(UIGenerator.GetSupportTypeName(0)))) { this.typeName = "UI" + this.typeName; warn = true; } this.baseType = this.baseTypePopup.SelectedItem; var config = EditorHelper.GetOrCreateScriptableObject <UIManConfig>(false); var savePath = ""; if (this.baseType.Equals(UIGenerator.GetSupportTypeName(0))) { savePath = config.modelScriptFolder; config.generatingTypeIsDialog = false; } else if (this.baseType.Equals(UIGenerator.GetSupportTypeName(1))) { savePath = config.screenScriptFolder; config.generatingTypeIsDialog = false; } else if (this.baseType.Equals(UIGenerator.GetSupportTypeName(2))) { savePath = config.dialogScriptFolder; config.generatingTypeIsDialog = true; } savePath = Application.dataPath + "/" + savePath + "/" + this.typeName + ".cs"; if (File.Exists(savePath) || UIGenerator.IsViewModelExisted(this.typeName)) { EditorUtility.DisplayDialog("Error", "A class of the same name has already existed", "OK"); return; } var paths = Regex.Split(savePath, "/"); var inheritance = string.Empty; if (this.baseType != this.arrSupportType[0]) { config.generatingType = this.typeName; } else { inheritance = $" : {this.baseType}"; } var code = UIManCodeGenerator.GenerateType(this.typeName, inheritance, false, config, this.namespaceField.Text); UIManCodeGenerator.SaveScript(savePath, code, true); if (this.baseType != this.arrSupportType[0]) { GenerateHandler(savePath); } AssetDatabase.Refresh(ImportAssetOptions.Default); if (warn) { UnuLogger.LogWarning("Code generation warning: Class name is invalid. New name is generated."); } Close(); }