Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using AC;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- [System.Serializable]
- public class ActionVariableCopyLocalized : Action
- {
- public int sourceGlobalVarID = -1;
- public int targetGlobalVarID = -1;
- public ActionVariableCopyLocalized()
- {
- this.isDisplayed = true;
- category = ActionCategory.Custom;
- title = "CopyVariableLocalized";
- description = "Copies the localized version of a string variable";
- }
- public override float Run()
- {
- if (KickStarter.runtimeVariables == null || KickStarter.speechManager == null || KickStarter.runtimeLanguages == null)
- {
- Debug.LogError("Adventure Creator components not initialized.");
- return 0f;
- }
- GVar sourceVar = GlobalVariables.GetVariable(sourceGlobalVarID);
- GVar targetVar = GlobalVariables.GetVariable(targetGlobalVarID);
- if (sourceVar == null || targetVar == null)
- {
- Debug.LogWarning("Source or target global variable not found.");
- return 0f;
- }
- if (sourceVar.type != VariableType.String || targetVar.type != VariableType.String)
- {
- Debug.LogWarning("This action supports only string-type global variables.");
- return 0f;
- }
- string sourceValue = sourceVar.TextValue;
- // Find SpeechLine matching sourceValue in original language (usually language 0)
- SpeechLine matchedLine = null;
- foreach (SpeechLine line in KickStarter.speechManager.lines)
- {
- if (line.text.Trim() == sourceValue.Trim())
- {
- matchedLine = line;
- break;
- }
- }
- if (matchedLine == null)
- {
- Debug.LogWarning("No matching SpeechLine found for source variable text.");
- targetVar.TextValue = sourceValue; // fallback to raw copy
- return 0f;
- }
- int languageIndex = Options.GetLanguage();
- string localizedText = KickStarter.runtimeLanguages.GetTranslation(matchedLine.text, matchedLine.lineID, languageIndex);
- if (string.IsNullOrEmpty(localizedText))
- {
- Debug.LogWarning("Localized text not found; copying raw text.");
- localizedText = sourceValue;
- }
- // Copy localized text to target variable
- targetVar.TextValue = localizedText;
- return 0f;
- }
- #if UNITY_EDITOR
- public override void ShowGUI()
- {
- sourceGlobalVarID = AdvGame.GlobalVariableGUI("Source global string variable:", sourceGlobalVarID, VariableType.String);
- targetGlobalVarID = AdvGame.GlobalVariableGUI("Target global string variable:", targetGlobalVarID, VariableType.String);
- }
- public override string SetLabel()
- {
- return $"Copy localized text from GVar {sourceGlobalVarID} to GVar {targetGlobalVarID}";
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment