// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using System; using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEngine; namespace Doozy.Editor.Common { #if DOOZY_42 [CreateAssetMenu(menuName = "Doozy/42/Product Info", fileName = "ProductInfo", order = 1000)] #endif [Serializable] public class ProductInfo : ScriptableObject { public const string k_DefaultID = "N/A"; public string Id = k_DefaultID; public string Category; public string Name; [Space(5)] public int Major; public int Minor; public int RevisionVersion; [HideInInspector] public int BuildVersion; public Version version => new Version(Major, Minor, BuildVersion, RevisionVersion); /// Construct a new ProductInfo with the given values /// Product category /// Name of the product /// Major Version number: Incompatible API changes /// Minor Version number: Functionality added in a backwards compatible manner /// Patch Version number: Backwards compatible bug fixes public ProductInfo(string category, string productName, int major, int minor, int revision) { Category = category; Name = productName; Major = major; Minor = minor; RevisionVersion = revision; BuildVersion = 1; } /// Product category without any whitespaces public string safeCategory => Category.Replace(" ", ""); /// Name of the product without any whitespaces public string safeName => Name.Replace(" ", ""); /// /// Name of the product (with whitespaces) and its version /// {Name} {Major}.{Minor}.{Patch} /// public string nameAndVersion => $"{Name} {Major}.{Minor}.{RevisionVersion}"; /// /// Product category, name of the product (with whitespaces) and its version /// {Category} {Name} {Major}.{Minor}.{Patch} /// public string categoryAndNameAndVersion => $"{Category} {nameAndVersion}"; public override string ToString() => nameAndVersion; /// Search the project for all the ProductDetails assets with the given category /// Product category to search for public static List GetProductCategory(string category) { var list = new List(); #if UNITY_EDITOR string[] guids = AssetDatabase.FindAssets($"t:{nameof(ProductInfo)}"); if (guids == null) { Debug.Log($"No {nameof(ProductInfo)} asset was found in this project"); return list; } foreach (string guid in guids) { string assetPath = AssetDatabase.GUIDToAssetPath(guid); ProductInfo asset = AssetDatabase.LoadAssetAtPath(assetPath); if (asset == null || list.Contains(asset) || !asset.Category.Equals(category)) continue; list.Add(asset); } return list; #else Debugger.Log("Cannot get this information outside the Unity Editor"); return list; #endif } /// Search the project for all the ProductDetails assets and get the first one with the given category and productName /// Product category to search for /// Name of the product to search for public static ProductInfo Get(string category, string productName) { #if UNITY_EDITOR string[] guids = AssetDatabase.FindAssets($"t:{nameof(ProductInfo)}"); if (guids != null) return guids.Select(AssetDatabase.GUIDToAssetPath) .Select(AssetDatabase.LoadAssetAtPath) .FirstOrDefault(asset => asset != null && asset.Category.Equals(category) && asset.Name.Equals(productName)); Debug.Log($"No {nameof(ProductInfo)} asset was found in this project"); return null; #else Debugger.Log("Cannot get this information outside the Unity Editor"); return null; #endif } /// Search the project for all the ProductDetails assets and get the first one with the given productName /// Name of the product to search for public static ProductInfo Get(string productName) { #if UNITY_EDITOR string[] guids = AssetDatabase.FindAssets($"t:{nameof(ProductInfo)}"); if (guids != null) return guids.Select(AssetDatabase.GUIDToAssetPath) .Select(AssetDatabase.LoadAssetAtPath) .Where(asset => asset != null) .FirstOrDefault(asset => asset.Name.Equals(productName)); Debug.Log($"No {nameof(ProductInfo)} asset was found in this project"); return null; #else Debugger.Log("Cannot get this information outside the Unity Editor"); return null; #endif } } }