コード例 #1
0
        //
        // ─── TEXT COMPONENT REPLACEMENT ──────────────────────────────────
        //

        #region TEXT COMPONENT REPLACEMENT

        private void ReplaceTextComponent(ReplaceUnit updatedReference)
        {
            TextInformation textInfo = updatedReference.textInformation;

            // * Don't even think of performing below operations on previously saved prefabs loaded into the memory
            // * They are like lost souls that want to trap your innocent code
            // * Whatever you execute on them gets lost in a limbo and flushed down along the garbage collection
            // * If you want to edit a prefab, make sure you just loaded it and you work on a fresh, crunchy instance

            using (var editScope = new EditPrefabAssetScope(updatedReference.prefabPath))
            {
                GameObject      root      = editScope.prefabRoot;
                TextMeshProUGUI tmProText = GetTMProText(updatedReference, textInfo, root);
                textInfo.StyleTMProText(tmProText, _fontAssetMap);

                if (updatedReference.isReferenced)
                {
                    TMProAdapter tmProAdapter = GetTextAdapter(updatedReference, root, tmProText);
                    if (tmProAdapter == null)
                    {
                        return;
                    }
                    AssignTMProReference(updatedReference, tmProAdapter, root);
                }
            }
        }
コード例 #2
0
        private static void SetAdapterAndTMProFieldValues(TMProAdapter tmProAdapter, ReplaceUnit replaceUnit, object fieldOwner, FieldInfo adapterFieldInfo, FieldInfo tmProFieldInfo)
        {
            FieldInformation fieldInformation = replaceUnit.fieldInformation;
            FieldType        fieldType        = fieldInformation.FieldType;

            if (fieldType.HasOneOfTheFlags(FieldType.Listed | FieldType.Arrayed))
            {
                var adpaterField = adapterFieldInfo.GetValue(fieldOwner);
                var tmProField   = tmProFieldInfo.GetValue(fieldOwner);

                EnumerableFieldInformation efi = fieldInformation.GetFieldInformationParamter <EnumerableFieldInformation>();

                if (adpaterField != null && tmProField != null)
                {
                    if (adpaterField is List <TMProAdapter> adapterList)
                    {
                        adapterList[efi.index] = tmProAdapter;
                        (tmProField as List <TextMeshProUGUI>).Add(tmProAdapter.TMProText);
                    }
                    else if (adpaterField is TMProAdapter[] adapterArray)
                    {
                        adapterArray[efi.index] = tmProAdapter;

                        //* We don't need to do the same with above adapterArray since it already has the correct size as it
                        //* replaced the old array of text components
                        TextMeshProUGUI[] tmproFieldArray    = tmProField as TextMeshProUGUI[];
                        TextMeshProUGUI[] newTMProFieldArray = new TextMeshProUGUI[tmproFieldArray.Length + 1];
                        tmproFieldArray.CopyTo(newTMProFieldArray, 0);
                        newTMProFieldArray[tmproFieldArray.Length] = tmProAdapter.TMProText;
                        tmProFieldInfo.SetValue(fieldOwner, newTMProFieldArray);
                    }
                    else
                    {
                        Debug.LogError($"Huh? Thats weird");
                    }
                }
                else
                {
                    Debug.LogError($"Either adapter field: {adpaterField} or tmpro field: {tmProField} is null.");
                }
            }
            else
            {
                adapterFieldInfo.SetValue(fieldOwner, tmProAdapter);
                tmProFieldInfo.SetValue(fieldOwner, tmProAdapter.TMProText);
            }
        }
コード例 #3
0
        private static TMProAdapter GetTextAdapter(ReplaceUnit updatedReference, GameObject root, TextMeshProUGUI newTextComponent)
        {
            TMProAdapter adapter = null;

            try
            {
                string fieldOwnerName    = updatedReference.fieldInformation.FieldOwnerType.Name;
                string adapterParentName = String.Format(ADAPTER_PARENT_NAME, fieldOwnerName);

                GameObject adaptersParent = GetAdaptersParent(root, adapterParentName);
                adapter = GetOrCreateAdapter(updatedReference, newTextComponent, adaptersParent);
            }
            catch (Exception ex)
            {
                Debug.LogError($"Exception occured for {updatedReference.rootPrefab} at path {updatedReference.prefabPath}, message:  {ex.Message}");
            }

            return(adapter);
        }
コード例 #4
0
        private void AssignTMProReference(ReplaceUnit reference, TMProAdapter tmProAdapter, GameObject root)
        {
            try
            {
                if (tmProAdapter == null)
                {
                    Debug.LogError($"Adapter is null for {reference.prefabPath} field {reference.fieldInformation.FieldName}");
                }

                object fieldOwner = GetFieldOwner(reference, root);

                GetAdapterAndTmproFieldInfos(
                    reference.fieldInformation,
                    out FieldInfo adapterFieldInfo,
                    out FieldInfo tmProFieldInfo);

                SetAdapterAndTMProFieldValues(tmProAdapter, reference, fieldOwner, adapterFieldInfo, tmProFieldInfo);
            }
            catch (Exception ex)
            {
                Debug.LogError($"Exception occured for {reference.rootPrefab} at path {reference.prefabPath}, message:  {ex.Message}");
            }
        }
 public void OnEnable()
 {
     _adapter = (TMProAdapter)target;
 }