简体   繁体   English

Unity ScriptableObjects 和字符串插值

[英]Unity ScriptableObjects and String Interpolation

I am sure its possible, but I haven't figured out how.我确信它可能,但我还没有弄清楚如何。 How would I go about using string interpolation for scriptableObjects?我将如何为 scriptableObjects 使用字符串插值? Some basic code I have for it:我有一些基本代码:

    public class ScriptableCardBase : ScriptableObject
    {
      public string cardDes;
    }
        
     public class CardUI : MonoBehaviour
    {
        public List<ScriptableCardBase> card = new List<ScriptableCardBase>();
        
        public Text desText;
        
        public void DisplayCard(int i)
        {
         desText.text = card[i].cardDes;
        }
    }

For my cards, I just want to be able to display the damage of the card, or number of effects, etc. Here is a screenshot of what the SO Card looks like and where I try to input the string.对于我的卡片,我只想能够显示卡片的伤害,或效果数量等。这是 SO Card 的外观以及我尝试输入字符串的位置的屏幕截图。 Any help would be greatly appreciated!任何帮助将不胜感激!

CardSO卡SO

I guess a long questions short is, can I use string interpolation with scriptable objects?我想一个很长的问题是,我可以对可编写脚本的对象使用字符串插值吗?

What you want is to convert your string to its Formattable version.你想要的是将你的字符串转换成它的 Formattable 版本。

You can do it via reflection or some sort of IL you want.您可以通过反射或您想要的某种 IL 来做到这一点。 Some start could be:一些开始可能是:

public static string ParseFromFormattable(this string str, object context) {
    // Define a random char that is unlikely to appear on normal text.
    const char weirdChar = '°'; // This will prepend the interpolation parameters

    var outputStr = "";

    // Split all the parts and iterate through them, using reflection to get the value from values inside '{}'
    foreach (var part in GetStringParts()) {
        if (part[0] == weirdChar) { outputStr += context.GetType().GetField(part.Substring(1)).GetValue(context).ToString(); }
        else { outputStr += part; }
    }
    return "";


    List<string> GetStringParts() {
        var strParts = new List<string>();
        var currentPart = "";
        var writingParam = false;

        foreach (var c in str) {
            if (c == '{') {
                if (currentPart != "") { strParts.Add(currentPart); }
                currentPart = "";
                writingParam = true;
            }
            else if (writingParam && c == '}') {
                strParts.Add(weirdChar + currentPart);
                currentPart = "";
                writingParam = false;
            }
            else { currentPart += c; }
        }
        strParts.Add(currentPart); // Don't forget to add the last string
        return strParts;
    }
}

Note this is a very basic version that doesn't support complex interpolation parameters.. Only parameters that are field members of the class that is passed as 'context' will be supported.请注意,这是一个非常基本的版本,不支持复杂的插值参数。仅支持作为“上下文”传递的类的字段成员的参数。

You can expand the reflection parts to support propeties, methods, and references if you want.. or even direct code (math operations, etc).如果需要,您可以扩展反射部分以支持属性、方法和引用……甚至是直接代码(数学运算等)。 I would highly recommend just using some IL instead, though.不过,我强烈建议只使用一些 IL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM