Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Reading an object variable sent with sendMessage

Discussion in 'Flash' started by Miyavi, Jul 24, 2012.

  1. Miyavi

    Miyavi

    Joined:
    Mar 10, 2012
    Posts:
    58
    Hi,

    I have two projects successfully communicated, as in the Unity part, I have a function with a GUIText (in which is written "Model: +variable, being variable a number" which I call in the AS3 part.
    My sendMessage function in AS3 is as follows: sendMessage("Object", "SetModel", {foo:1});
    So yeah, I want to send a variable named "foo" with the value of "1".

    Now, when I run the project, the function "SetModel" I call, works partially, as "Model: 0" is written, even when the value from my sendMessage anon object is not 0, so I guess it's me failing at reading the object from Unity.

    Could anyone lend me a hand with this?
    I've start reading about Reflection and also about the Actionscript.Statement function, but I don't really know if these are needed, or if I could do it easily another way.
     
  2. ParaLogic

    ParaLogic

    Joined:
    Aug 19, 2010
    Posts:
    177
    Yup, Unity can't read the object. You can work around it like this:

    1. In the Actionscript folder in your assets, create an .as file called ObjectWorkaround.as and paste the code below in it:

    Code (csharp):
    1. package  {
    2.    
    3.     public class ObjectWorkaround {
    4.        
    5.         public static function returnString(obj : Object, param : String):String{
    6.             return obj[param];
    7.         }
    8.        
    9.         public static function returnInt(obj : Object, param : String):int{
    10.             return obj[param];
    11.         }
    12.  
    13.     }
    14.    
    15. }
    2. Then create a C# script called ObjectWorkaround.cs in the Plugins folder and paste the following code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [NotConverted]
    5. [NotRenamed]
    6.  
    7. public class ObjectWorkaround {
    8.  
    9.     public static string returnString(object o, string param){
    10.         Debug.Log ("This won't work until exported to a SWF");
    11.         return null;
    12.     }
    13.    
    14.     public static int returnInt(object o, string param){
    15.         Debug.Log ("This won't work until exported to a SWF");
    16.         return 0;
    17.     }
    18. }
    19.  
    Now in the function you call from flash, simply use ObjectWorkaround.returnInt(Object, "IntName"); to get an integer and use ObjectWorkaround.returnInt(Object, "StingName"); to get back a string value.

    So in your case:

    Code (csharp):
    1.  
    2. public static function SetModel(object o):void{
    3.            // To get the integer value
    4.            int yourFooValue = ObjectWorkaround.returnInt(o, "foo");
    5. }
    6.  
    Hope that helps, again ;)
     
  3. Miyavi

    Miyavi

    Joined:
    Mar 10, 2012
    Posts:
    58
    Thanks a lot, I'm going to try that now :D
    Will be back with the results.
     
  4. Miyavi

    Miyavi

    Joined:
    Mar 10, 2012
    Posts:
    58
    So... right now, I'm getting this error.
    I had to create the Actionscript folder as there was none in my project, but I don't know what the problem could be or where did I mess up.


    PS: Nevermind, got it working!
    Thanks a lot!
     
    Last edited: Jul 24, 2012
  5. ParaLogic

    ParaLogic

    Joined:
    Aug 19, 2010
    Posts:
    177
    No problem, glad to help :)
     
  6. Miyavi

    Miyavi

    Joined:
    Mar 10, 2012
    Posts:
    58
    Now... is there any way to load the unity content in a new flash window?

    I already tried with the NativeWindow class, but it seems it only runs for an AIR project, and running this project under AIR won't load the Unity content.

    If I can't open a new window, could I at least make the content load and be bounded to an MovieClip?
    I create a mc with a width and height, use mc.addChild(loader.content) when the content is loaded, but the content won't be limited by the mc.

    PS: I'm also trying to change the size of the content,which should be done in the html file it generates. There's a function in there called swfobject.embedSWF, which has a width and heigh. I change it, and it doesn't matter as it doesn't resize at all.
     
    Last edited: Jul 25, 2012