Last active
September 18, 2018 21:53
-
-
Save DenchiSoft/2c13dbd41267c4b26caaea9335ec3183 to your computer and use it in GitHub Desktop.
Extension method for CubismModel that makes it easy to access CubismParameters by name. CubismParameters for each model are stored in a dictionary the first time they're needed for fast access next time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // CubismModel model = ...; | |
| // Call it like this: | |
| void Start() { | |
| // ... | |
| CubismParameter myParam = model.GetParam("ParamIDName"); | |
| // ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using Live2D.Cubism.Core; | |
| using UnityEngine; | |
| public static class CubismExtensions | |
| { | |
| // Outer dictionary key is model name. | |
| // Inner dictionary key is parameter name. | |
| private static Dictionary<string, Dictionary<string, CubismParameter>> live2DModelParams = new Dictionary<string, Dictionary<string, CubismParameter>>(); | |
| /// <summary> | |
| /// Returns the <see cref="CubismParameter"/> of this model with the given name. | |
| /// Will return <see langword="null" /> if no parameter with this name is found in the model. | |
| /// </summary> | |
| /// <param name="model"><see langword="this"/>.</param> | |
| /// <param name="paramName">The parameter name.</param> | |
| public static CubismParameter GetParam(this CubismModel model, string paramName) | |
| { | |
| Dictionary<string, CubismParameter> specificParams; | |
| if (!live2DModelParams.TryGetValue(model.name, out specificParams)) | |
| { | |
| // Model not known. Create Dictionary of CubismParameters. | |
| specificParams = new Dictionary<string, CubismParameter>(); | |
| foreach (CubismParameter param in model.Parameters) | |
| { | |
| specificParams.Add(param.Id, param); | |
| } | |
| live2DModelParams.Add(model.name, specificParams); | |
| } | |
| // Model (now) known. | |
| CubismParameter requestedParameter; | |
| if (specificParams.TryGetValue(paramName, out requestedParameter)) | |
| { | |
| return requestedParameter; | |
| } | |
| // Parameter not found in model. | |
| Debug.LogError("Parameter \"" + paramName + "\" not found for model \"" + model.name + "\"."); | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment