Search Unity

cannot seem to send message to script.

Discussion in 'Android' started by zergmouse, May 23, 2014.

  1. zergmouse

    zergmouse

    Joined:
    Jun 30, 2010
    Posts:
    216
    Can anyone tell me why this is not working? I am trying to set the text of the GUIText when a gesture is recognized. I am trying to use

    Code (csharp):
    1. UnityPlayer.UnitySendMessage("GUIText", "SetText", "Something");
    Here is the full script:
    Code (csharp):
    1. /*============================================================================
    2. Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
    3. All Rights Reserved.
    4. ============================================================================*/
    5.  
    6.  
    7. package com.qualcomm.QCARUnityPlayer;
    8.  
    9. import android.app.Dialog;
    10. import android.app.NativeActivity;
    11. import android.content.res.Configuration;
    12. import android.graphics.PixelFormat;
    13. import android.os.Bundle;
    14. import android.view.KeyEvent;
    15. import android.view.MotionEvent;
    16. import android.view.View;
    17. import android.view.Window;
    18. import android.view.WindowManager;
    19. import java.lang.reflect.Method;
    20. import com.unity3d.player.UnityPlayer;
    21.  
    22. import com.google.android.glass.touchpad.Gesture;
    23. import com.google.android.glass.touchpad.GestureDetector;
    24.  
    25. /** This custom NativeActivity shows how to initialize QCAR together with Unity from an activity.
    26. *   If you need to integrate another native library, you can modifiy this code and
    27. *   compile it to a JAR file to replace QCARUnityPlayer.jar in Assets/Plugins/Android
    28. *
    29. */
    30.  
    31. public class QCARPlayerNativeActivity extends NativeActivity
    32. {
    33.     private QCARPlayerSharedActivity mQCARShared;
    34.  
    35.     protected UnityPlayer mUnityPlayer;
    36.     // don't change the name of this variable; referenced from native code
    37.     // UnityPlayer.init() should be called before attaching the view to a layout - it will load the native code.
    38.     // UnityPlayer.quit() should be the last thing called - it will unload the native code.
    39.  
    40.     private GestureDetector mGestureDetector;
    41.    
    42.     protected void onCreate (Bundle savedInstanceState) {
    43.         requestWindowFeature(Window.FEATURE_NO_TITLE);
    44.         mUnityPlayer = new UnityPlayer(this);
    45.         super.onCreate(savedInstanceState);
    46.  
    47.         // initialize QCAR asynchronously
    48.         mQCARShared = new QCARPlayerSharedActivity();
    49.         int gles_mode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
    50.         mQCARShared.onCreate(this, gles_mode, new UnityInitializer());
    51.         getWindow().takeSurface(null);
    52.         setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
    53.         getWindow().setFormat(PixelFormat.RGB_565);
    54.         if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))
    55.             getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
    56.                                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    57.                                    
    58.        
    59.         //calling gestures for glass
    60.         mGestureDetector = createGestureDetector(this);
    61.     }
    62.    
    63.     private class UnityInitializer implements QCARPlayerSharedActivity.IUnityInitializer{
    64.         public void InitializeUnity(){
    65.             // Test for init method to see if this is 4.5 or earlier
    66.             try {
    67.                 Class<?> cls = Class.forName("com.unity3d.player.UnityPlayer");
    68.                 Method method = cls.getMethod("init", Integer.TYPE, Boolean.TYPE);
    69.                 int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);
    70.                 boolean trueColor8888 = false;
    71.                 method.invoke(mUnityPlayer, glesMode, trueColor8888);
    72.             }
    73.             catch (Exception e) { }
    74.             View playerView = mUnityPlayer.getView();
    75.             setContentView(playerView);
    76.             playerView.requestFocus();
    77.             //
    78.             UnityPlayer.UnitySendMessage("GUIText", "SetText", "Something");
    79.         }
    80.     }
    81.  
    82.     public boolean onGenericMotionEvent(MotionEvent event) {
    83.         // Send gestures to the gesture event for processing
    84.         if (mGestureDetector != null) {
    85.             return mGestureDetector.onMotionEvent(event);
    86.         }
    87.         return false;
    88.     }  
    89.     //Attempting to find action on the glass and display the text.
    90.     private GestureDetector createGestureDetector(Context context) {
    91.     GestureDetector gestureDetector = new GestureDetector(context);
    92.         //Create a base listener for generic gestures
    93.         gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
    94.             @Override
    95.             public boolean onGesture(Gesture gesture) {
    96.                 //
    97.                 UnityPlayer.UnitySendMessage("GUIText", "SetText", "Something");
    98.                 return true;
    99.             }
    100.         });
    101.         gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
    102.             @Override
    103.             public void onFingerCountChanged(int previousCount, int currentCount) {
    104.               // do something on finger count changes
    105. //              simCard.setText("Finger Check: "+currentCount);
    106. //              simCard.setFootnote("Previous Finger Count: "+previousCount);
    107. //              SetCardToView(simCard, simView);
    108.             }
    109.         });
    110.         gestureDetector.setScrollListener(new GestureDetector.ScrollListener() {
    111.             @Override
    112.             public boolean onScroll(float displacement, float delta, float velocity) {
    113.                 // do something on scrolling
    114.                 return false;
    115.             }
    116.         });
    117.         return gestureDetector;
    118.     }    
    119.    
    120.     protected void onDestroy (){
    121.         mQCARShared.onDestroy();
    122.         mUnityPlayer.quit();
    123.         super.onDestroy();
    124.     }
    125.  
    126.     // onPause()/onResume() must be sent to UnityPlayer to enable pause and resource recreation on resume.
    127.     protected void onPause(){
    128.         super.onPause();
    129.         mUnityPlayer.pause();
    130.         mQCARShared.onPause();
    131.     }
    132.     protected void onResume(){
    133.         super.onResume();
    134.         mQCARShared.onResume();
    135.         mUnityPlayer.resume();
    136.     }
    137.     public void onConfigurationChanged(Configuration newConfig){
    138.         super.onConfigurationChanged(newConfig);
    139.         mUnityPlayer.configurationChanged(newConfig);
    140.     }
    141.     public void onWindowFocusChanged(boolean hasFocus)  {
    142.         super.onWindowFocusChanged(hasFocus);
    143.         mUnityPlayer.windowFocusChanged(hasFocus);
    144.     }
    145.     public boolean dispatchKeyEvent(KeyEvent event) {
    146.         if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
    147.             return mUnityPlayer.onKeyMultiple(event.getKeyCode(), event.getRepeatCount(), event);
    148.         return super.dispatchKeyEvent(event);
    149.     }
    150.  
    151. }
    152.  
    I guess I should also mention that I am deploying this to Google Glass. The program works fine, the Augmented reality stuff all works. Also, I can set the GUIText via a script on another game object. I just cannot set it from the Java. Thoughts?

    Any help would be appreciated...
     
  2. zergmouse

    zergmouse

    Joined:
    Jun 30, 2010
    Posts:
    216
    I guess I should also mention that I tried
    Code (csharp):
    1. mUnityPlayer.UnitySendMessage("GUIText", "SetText", "Something");
    and that did not work. Where can I put this call to test it out? Am I putting it in the wrong place? you can see I tried to put it after Unity gets called to see if I could set the text, no luck.