Guest User

Untitled

a guest
Jul 22nd, 2025
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using UnityEngine;
  2. using AC;
  3.  
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7.  
  8. [System.Serializable]
  9. public class ActionVariableCopyLocalized : Action
  10. {
  11.     public int sourceGlobalVarID = -1;
  12.     public int targetGlobalVarID = -1;
  13.  
  14.     public ActionVariableCopyLocalized()
  15.     {
  16.         this.isDisplayed = true;
  17.         category = ActionCategory.Custom;
  18.         title = "CopyVariableLocalized";
  19.         description = "Copies the localized version of a string variable";
  20.     }
  21.  
  22.     public override float Run()
  23.     {
  24.         if (KickStarter.runtimeVariables == null || KickStarter.speechManager == null || KickStarter.runtimeLanguages == null)
  25.         {
  26.             Debug.LogError("Adventure Creator components not initialized.");
  27.             return 0f;
  28.         }
  29.  
  30.         GVar sourceVar = GlobalVariables.GetVariable(sourceGlobalVarID);
  31.         GVar targetVar = GlobalVariables.GetVariable(targetGlobalVarID);
  32.  
  33.         if (sourceVar == null || targetVar == null)
  34.         {
  35.             Debug.LogWarning("Source or target global variable not found.");
  36.             return 0f;
  37.         }
  38.  
  39.         if (sourceVar.type != VariableType.String || targetVar.type != VariableType.String)
  40.         {
  41.             Debug.LogWarning("This action supports only string-type global variables.");
  42.             return 0f;
  43.         }
  44.  
  45.         string sourceValue = sourceVar.TextValue;
  46.  
  47.         // Find SpeechLine matching sourceValue in original language (usually language 0)
  48.         SpeechLine matchedLine = null;
  49.         foreach (SpeechLine line in KickStarter.speechManager.lines)
  50.         {
  51.             if (line.text.Trim() == sourceValue.Trim())
  52.             {
  53.                 matchedLine = line;
  54.                 break;
  55.             }
  56.         }
  57.  
  58.         if (matchedLine == null)
  59.         {
  60.             Debug.LogWarning("No matching SpeechLine found for source variable text.");
  61.             targetVar.TextValue = sourceValue; // fallback to raw copy
  62.             return 0f;
  63.         }
  64.  
  65.         int languageIndex = Options.GetLanguage();
  66.  
  67.         string localizedText = KickStarter.runtimeLanguages.GetTranslation(matchedLine.text, matchedLine.lineID, languageIndex);
  68.  
  69.         if (string.IsNullOrEmpty(localizedText))
  70.         {
  71.             Debug.LogWarning("Localized text not found; copying raw text.");
  72.             localizedText = sourceValue;
  73.         }
  74.  
  75.         // Copy localized text to target variable
  76.         targetVar.TextValue = localizedText;
  77.  
  78.  
  79.         return 0f;
  80.     }
  81.  
  82. #if UNITY_EDITOR
  83.  
  84.     public override void ShowGUI()
  85.     {
  86.         sourceGlobalVarID = AdvGame.GlobalVariableGUI("Source global string variable:", sourceGlobalVarID, VariableType.String);
  87.         targetGlobalVarID = AdvGame.GlobalVariableGUI("Target global string variable:", targetGlobalVarID, VariableType.String);
  88.     }
  89.  
  90.     public override string SetLabel()
  91.     {
  92.         return $"Copy localized text from GVar {sourceGlobalVarID} to GVar {targetGlobalVarID}";
  93.     }
  94.  
  95. #endif
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment