/// <summary> /// Value converter for Vector3 type. /// </summary> public override ConversionResult Convert(object value, ValueConverterContext context) { if (value == null) { return(base.Convert(value, context)); } Type valueType = value.GetType(); if (valueType == _type) { return(base.Convert(value, context)); } else if (valueType == _stringType) { var stringValue = (string)value; float[] valueList; try { valueList = stringValue.Split(',').Select(x => System.Convert.ToSingle(x, CultureInfo.InvariantCulture)).ToArray(); } catch (Exception e) { return(ConversionFailed(value, e)); } if (valueList.Length == 1) { return(new ConversionResult(new Vector3(valueList[0], valueList[0], valueList[0]))); } else if (valueList.Length == 2) { return(new ConversionResult(new Vector3(valueList[0], valueList[1]))); } else if (valueList.Length == 3) { return(new ConversionResult(new Vector3(valueList[0], valueList[1], valueList[2]))); } else { return(StringConversionFailed(value)); } } return(ConversionFailed(value)); }
/// <summary> /// Value converter for float type. /// </summary> public override ConversionResult Convert(object value, ValueConverterContext context) { if (value == null) { return(base.Convert(value, context)); } Type valueType = value.GetType(); if (valueType == _type) { return(base.Convert(value, context)); } else if (valueType == _stringType) { var stringValue = (string)value; try { var convertedValue = System.Convert.ToSingle(stringValue, CultureInfo.InvariantCulture); return(new ConversionResult(convertedValue)); } catch (Exception e) { return(ConversionFailed(value, e)); } } else { // attempt to convert using system type converter try { var convertedValue = System.Convert.ToSingle(value, CultureInfo.InvariantCulture); return(new ConversionResult(convertedValue)); } catch (Exception e) { return(ConversionFailed(value, e)); } } }
/// <summary> /// Value converter for Color32 type. /// </summary> public override ConversionResult Convert(object value, ValueConverterContext context) { if (value == null) { return(base.Convert(value, context)); } Type valueType = value.GetType(); if (valueType == _type) { return(base.Convert(value, context)); } var result = _colorValueConverter.Convert(value, context); if (result.Success) { result.ConvertedValue = (Color32)((Color)result.ConvertedValue); } return(result); }
/// <summary> /// Value converter for ElementSize type. /// </summary> public override ConversionResult Convert(object value, ValueConverterContext context) { if (value == null) { return(base.Convert(value, context)); } Type valueType = value.GetType(); if (valueType == _type) { return(base.Convert(value, context)); } else if (valueType == _stringType) { var stringValue = (string)value; try { var convertedValue = ElementSize.Parse(stringValue, context.UnitSize); return(new ConversionResult(convertedValue)); } catch (Exception e) { return(ConversionFailed(value, e)); } } else if (valueType == _intType) { return(new ConversionResult(ElementSize.FromPixels((float)value))); } else if (valueType == _floatType) { return(new ConversionResult(ElementSize.FromPixels((float)value))); } return(ConversionFailed(value)); }
/// <summary> /// Value converter for Sprite type. /// </summary> public override ConversionResult Convert(object value, ValueConverterContext context) { if (value == null) { return(base.Convert(value, context)); } Type valueType = value.GetType(); if (valueType == _type) { return(base.Convert(value, context)); } else if (valueType == _stringType) { var stringValue = (string)value; try { string assetPath = stringValue.Trim(); UnityEngine.Object asset = null; if (String.IsNullOrEmpty(assetPath)) { return(new ConversionResult(null)); } if (!String.IsNullOrEmpty(context.BaseDirectory)) { assetPath = Path.Combine(context.BaseDirectory, assetPath); } // is asset pre-loaded? asset = ViewPresenter.Instance.GetSprite(assetPath); if (asset != null) { // yes. return pre-loaded asset return(new ConversionResult(asset)); } // if the asset is in a resources folder the load path should be relative to the folder bool inResourcesFolder = assetPath.Contains("Resources/"); string loadAssetPath = assetPath; if (inResourcesFolder) { loadAssetPath = loadAssetPath.Substring(assetPath.IndexOf("Resources/") + 10); string extension = System.IO.Path.GetExtension(assetPath); if (extension.Length > 0) { loadAssetPath = loadAssetPath.Substring(0, loadAssetPath.Length - extension.Length); } } // load asset from asset database if (!Application.isPlaying || inResourcesFolder) { // load sprite from asset database // does the path refer to a sprite in a sprite atlas? if (assetPath.Contains(":")) { // yes. load sprite from atlas string[] parts = loadAssetPath.Split(':'); string filepath = parts[0]; #if UNITY_EDITOR UnityEngine.Object[] subSprites = inResourcesFolder ? Resources.LoadAll(filepath) : AssetDatabase.LoadAllAssetRepresentationsAtPath(filepath); #else UnityEngine.Object[] subSprites = Resources.LoadAll(filepath); #endif foreach (var s in subSprites) { if (s.name == parts[1]) { asset = s; break; } } if (asset == null) { return(ConversionFailed(value, String.Format("Asset not found at path \"{0}\".", assetPath))); } // add asset to view-presenter ViewPresenter.Instance.AddSprite(assetPath, asset as Sprite); return(new ConversionResult(asset)); } else { // load sprite #if UNITY_EDITOR asset = inResourcesFolder ? Resources.Load(loadAssetPath, _type) : AssetDatabase.LoadAssetAtPath(loadAssetPath, _type); #else asset = Resources.Load(loadAssetPath, _type); #endif if (asset == null) { return(ConversionFailed(value, String.Format("Asset not found at path \"{0}\".", assetPath))); } ViewPresenter.Instance.AddSprite(assetPath, asset as Sprite); return(new ConversionResult(asset)); } } return(ConversionFailed(value, String.Format("Pre-loaded asset not found for path \"{0}\".", assetPath))); } catch (Exception e) { return(ConversionFailed(value, e)); } } return(ConversionFailed(value)); }
/// <summary> /// Used to extend the asset value converter with custom types. /// </summary> protected override ConversionResult ConvertCustomType(object value, Type valueType, ValueConverterContext context) { var spriteAsset = value as SpriteAsset; return(spriteAsset != null ? new ConversionResult(spriteAsset.Sprite) : ConversionFailed(value)); }
/// <summary> /// Value converter for Font type. /// </summary> public override ConversionResult Convert(object value, ValueConverterContext context) { if (value == null) { return(base.Convert(value, context)); } Type valueType = value.GetType(); if (valueType == _type) { return(base.Convert(value, context)); } else if (valueType == _stringType) { var stringValue = (string)value; try { string assetPath = stringValue.Trim(); if (String.IsNullOrEmpty(assetPath)) { return(new ConversionResult(null)); } bool isOnDemandLoaded = assetPath.StartsWith("?"); if (isOnDemandLoaded) { assetPath = assetPath.Substring(1); } else if (!String.IsNullOrEmpty(context.BaseDirectory)) { assetPath = Path.Combine(context.BaseDirectory, assetPath); } // is asset pre-loaded? var unityAsset = ViewPresenter.Instance.GetAsset(assetPath); if (unityAsset != null) { // yes. return pre-loaded asset return(ConvertAssetResult(unityAsset)); } // is asset to be loaded externally on-demand? if (isOnDemandLoaded) { // yes. unityAsset = ViewPresenter.Instance.AddAsset(assetPath, null); return(ConvertAssetResult(unityAsset)); } // if the asset is in a resources folder the load path should be relative to the folder bool inResourcesFolder = assetPath.Contains("Resources/"); string loadAssetPath = assetPath; if (inResourcesFolder) { loadAssetPath = loadAssetPath.Substring(assetPath.IndexOf("Resources/") + 10); string extension = System.IO.Path.GetExtension(assetPath); if (extension.Length > 0) { loadAssetPath = loadAssetPath.Substring(0, loadAssetPath.Length - extension.Length); } } bool suppressAssetNotFoundError = false; loadAssetPath = ConvertAssetPath(loadAssetPath, inResourcesFolder, out suppressAssetNotFoundError); // load asset from asset database if (!Application.isPlaying || inResourcesFolder) { UnityEngine.Object asset = null; // load font from asset database #if UNITY_EDITOR asset = inResourcesFolder ? Resources.Load(loadAssetPath, _loadType) : AssetDatabase.LoadAssetAtPath(loadAssetPath, _loadType); #else asset = Resources.Load(loadAssetPath); #endif if (asset == null) { return(suppressAssetNotFoundError ? new ConversionResult(null) : ConversionFailed(value, String.Format("Asset not found at path \"{0}\".", assetPath))); } var loadedAsset = ViewPresenter.Instance.AddAsset(assetPath, asset); return(ConvertAssetResult(loadedAsset)); } return(suppressAssetNotFoundError ? new ConversionResult(null) : ConversionFailed(value, String.Format("Pre-loaded asset not found for path \"{0}\".", assetPath))); } catch (Exception e) { return(ConversionFailed(value, e)); } } else if (valueType == _loadType) { // is asset pre-loaded? var unityAsset = ViewPresenter.Instance.GetAsset(value as UnityEngine.Object); if (unityAsset != null) { return(ConvertAssetResult(unityAsset)); // yes } // no. return new unity asset return(ConvertAssetResult(new UnityAsset(String.Empty, value as UnityEngine.Object))); } else { return(ConvertCustomType(value, valueType, context)); } }
/// <summary> /// Used to extend the asset value converter with custom types. /// </summary> protected virtual ConversionResult ConvertCustomType(object value, Type valueType, ValueConverterContext context) { return(ConversionFailed(value)); }
/// <summary> /// Value converter for Font type. /// </summary> public override ConversionResult Convert(object value, ValueConverterContext context) { if (value == null) { return(base.Convert(value, context)); } Type valueType = value.GetType(); if (valueType == _type) { return(base.Convert(value, context)); } else if (valueType == _stringType) { var stringValue = (string)value; try { string assetPath = stringValue.Trim(); UnityEngine.Object asset = null; if (String.IsNullOrEmpty(assetPath)) { return(new ConversionResult(null)); } if (!String.IsNullOrEmpty(context.BaseDirectory)) { assetPath = Path.Combine(context.BaseDirectory, assetPath); } // is asset pre-loaded? asset = ViewPresenter.Instance.GetFont(assetPath); if (asset != null) { // yes. return pre-loaded asset return(new ConversionResult(asset)); } // if the asset is in a resources folder the load path should be relative to the folder bool inResourcesFolder = assetPath.Contains("Resources/"); string loadAssetPath = assetPath; if (inResourcesFolder) { loadAssetPath = loadAssetPath.Substring(assetPath.IndexOf("Resources/") + 10); string extension = System.IO.Path.GetExtension(assetPath); if (extension.Length > 0) { loadAssetPath = loadAssetPath.Substring(0, loadAssetPath.Length - extension.Length); } } // load asset from asset database if (!Application.isPlaying || inResourcesFolder) { // load font from asset database #if UNITY_EDITOR asset = inResourcesFolder ? Resources.Load(loadAssetPath) : AssetDatabase.LoadAssetAtPath(loadAssetPath, _type); #else asset = Resources.Load(loadAssetPath); #endif if (asset == null) { return(ConversionFailed(value, String.Format("Asset not found at path \"{0}\".", assetPath))); } ViewPresenter.Instance.AddFont(assetPath, asset as Font); return(new ConversionResult(asset)); } return(ConversionFailed(value, String.Format("Pre-loaded asset not found for path \"{0}\".", assetPath))); } catch (Exception e) { return(ConversionFailed(value, e)); } } return(ConversionFailed(value)); }
/// <summary> /// Value converter for Margin type. /// </summary> public override ConversionResult Convert(object value, ValueConverterContext context) { if (value == null) { return(base.Convert(value, context)); } Type valueType = value.GetType(); if (valueType == _type) { return(base.Convert(value, context)); } else if (valueType == _stringType) { var stringValue = (string)value; try { string[] valueList; valueList = stringValue.Split(',').ToArray(); ElementMargin convertedValue = null; if (valueList.Length == 1) { convertedValue = new ElementMargin(ElementSize.Parse(valueList[0], context.UnitSize)); } else if (valueList.Length == 2) { convertedValue = new ElementMargin( ElementSize.Parse(valueList[0], context.UnitSize), ElementSize.Parse(valueList[1], context.UnitSize)); } else if (valueList.Length == 3) { convertedValue = new ElementMargin( ElementSize.Parse(valueList[0], context.UnitSize), ElementSize.Parse(valueList[1], context.UnitSize), ElementSize.Parse(valueList[2], context.UnitSize)); } else if (valueList.Length == 4) { convertedValue = new ElementMargin( ElementSize.Parse(valueList[0], context.UnitSize), ElementSize.Parse(valueList[1], context.UnitSize), ElementSize.Parse(valueList[2], context.UnitSize), ElementSize.Parse(valueList[3], context.UnitSize)); } else { return(StringConversionFailed(value)); } return(new ConversionResult(convertedValue)); } catch (Exception e) { return(ConversionFailed(value, e)); } } else { // attempt to convert using system type converter try { var convertedValue = System.Convert.ToInt32(value, CultureInfo.InvariantCulture); return(new ConversionResult(convertedValue)); } catch (Exception e) { return(ConversionFailed(value, e)); } } }