Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by hurjm1006 · Apr 07, 2021 at 02:47 AM · scripting problemassembly

Mono.Cecil not apply script after first play

I'm first at Mono.Cecil using for injection ui code.

So I got a probelm. It works fine when first start editor.

But when I try to change script which one inherit Monobehavior after first play in editor, it's not apply at all.

In ILspy the codes are change properly and when I restart editor that changes apply.

I have no idea how to do it.

PLZ HELP ME!!!!

Here is my weaving code.

     public static class UIWeaver
     {
         [InitializeOnLoadMethod]
         public static void OnInitializeOnLoadMethod()
         {
             CompilationPipeline.assemblyCompilationFinished += OnCompilationFinished;
 
             // We only need to run this once per session
             // after that, all assemblies will be weaved by the event
             if (!SessionState.GetBool("UIBINDER_WEAVED", false))
             {
                 // reset session flag
                 SessionState.SetBool("UIBINDER_WEAVED", true);
 
                 WeaveExistingAssemblies();
             }
         }
 
 
         public static void WeaveExistingAssemblies()
         {
             foreach (UnityAssembly assembly in CompilationPipeline.GetAssemblies())
             {
                 if (File.Exists(assembly.outputPath))
                 {
                     OnCompilationFinished(assembly.outputPath, new CompilerMessage[0]);
                 }
             }
 
 #if UNITY_2019_3_OR_NEWER
             EditorUtility.RequestScriptReload();
 #else
             UnityEditorInternal.InternalEditorUtility.RequestScriptReload();
 #endif
         }
 
         private static void OnCompilationFinished(string assemblyPath, CompilerMessage[] messages)
         {
             Stopwatch sw = new Stopwatch();
             sw.Start();
             List<Type> derivedTypes = new List<Type>();
             List<PropertyDefinition> properties = new List<PropertyDefinition>();
 
             bool isModified = false;
             #region Cecil
             AssemblyDefinition currentAssembly = AssemblyDefinition.ReadAssembly(assemblyPath, new ReaderParameters { ReadWrite = true, ReadSymbols = true });
             {
 
                 ModuleDefinition mainModule = currentAssembly.MainModule;
 
                 var p = currentAssembly.MainModule.Types.Where(o => o.IsClass).SelectMany(t => t.Properties);
                 if (p != null)
                     properties.AddRange(p);
 
                 foreach (PropertyDefinition pd in properties)
                 {
                     CustomAttribute attr = pd.CustomAttributes.SingleOrDefault(i => i.AttributeType.Name == nameof(UIBindable));
                     if (attr != null)
                     {
                         Mono.Cecil.Cil.MethodBody body = pd.SetMethod.Body;
 
                         ILProcessor processor = body.GetILProcessor();
 
                         var instructions = body.Instructions;
                         var targetInst = instructions.First();
 
                         processor.InsertBefore(targetInst, processor.Create(OpCodes.Nop));
                         processor.InsertBefore(targetInst, processor.Create(OpCodes.Ldarg_0));
                         processor.InsertBefore(targetInst, processor.Create(OpCodes.Ldstr, pd.Name));
                         processor.InsertBefore(targetInst, processor.Create(OpCodes.Ldarg_1));
 
                         Type gt;
                         switch (pd.PropertyType.MetadataType)
                         {
                             case MetadataType.Single:
                                 {
                                     gt = typeof(UIBindProxy<float>);
                                     break;
                                 }
                             case MetadataType.String:
                                 {
                                     gt = typeof(UIBindProxy<string>);
                                     break;
                                 }
                             case MetadataType.Int32:
                                 {
                                     gt = typeof(UIBindProxy<int>);
                                     break;
                                 }
                             default:
                                 {
                                     UnityDebug.LogError("Missing type " + pd.PropertyType.MetadataType);
                                     continue;
                                 }
                         }
 
                         MethodInfo gm = gt.GetMethod("UpdateValue");
                         MethodReference gr = mainModule.ImportReference(gm);
                         processor.InsertBefore(targetInst, processor.Create(OpCodes.Call, gr));
 
                         isModified = true;
                     }
                 }
 
                 if (isModified)
                 {
                     currentAssembly.Write(new WriterParameters { WriteSymbols = true });
                 }
             }
 
             #endregion
             UnityEngine.Debug.Log("UI Weaving time: " + sw.ElapsedMilliseconds);
             sw.Stop();
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

255 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Namespce Error!!!!!!!!! 0 Answers

Script not doing anything 1 Answer

How can i create a squad of soldiers in specific formation ? 2 Answers

How can I make a button appear after it collides with a trigger? (JavaScript) 0 Answers

Very strange results from Camera.WorldToScreenPoint() 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges