Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by stork · Mar 04, 2014 at 03:09 AM · androidsendmessageunityplayer

C# Script from Native Android Service

I have a native Android service running in the background that needs to interface with a Unity3D script. I'm aware of the following method:

com.unity3d.player.UnityPlayer.UnitySendMessage(String gameObject, String arg1, String arg2)

But it requires a gameObject parameter. Since the service is running in the background, I can't be 100% sure that a gameObject attached to a particular scene has not yet been destroyed. Is this a fair assumption (since the user might not even be in the app)?

If so, is there a way to instantiate a gameObject that is never destroyed? Is there any other way to call a unity script from native Java code?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Mar 04, 2014 at 03:10 AM

You need to create an object in your initial scene and use DontDestroyOnLoad(gameObject) on it.

Comment
Add comment · Show 6 · Share
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
avatar image stork · Mar 04, 2014 at 03:21 AM 0
Share

Is there any other way, so that it will still work if Android kills the game process (my service is running in a separate process)?

How do Unity apps trigger special behavior when push notifications occur? I assume doing something similar would work

avatar image whydoidoit · Mar 04, 2014 at 04:07 AM 0
Share

You can register an intent for your game which responds by starting it if it isn't already running - is that what you need?

avatar image whydoidoit · Mar 04, 2014 at 04:07 AM 0
Share

An intent would allow a url scheme to launch the game or some other action.

avatar image stork · Mar 04, 2014 at 04:38 AM 0
Share

Sounds like it. An intent coupled with your previous solution. How do you register/use an intent for the game? Thanks!

avatar image whydoidoit · Mar 04, 2014 at 07:01 AM 0
Share

Here's my core activity which registers intents and extends UnityPlayerActivity:

 package com.threeradical.urlscheme;
 
 import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
 
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.Package$$anonymous$$anager;
 import android.content.pm.ResolveInfo;
 import android.net.Uri;
 import android.os.Bundle;
 import android.util.Log;
 
 import com.unity3d.player.UnityPlayer;
 import com.unity3d.player.UnityPlayerNativeActivity;
 
 
 public class UrlSchemeNativePlayerActivity extends UnityPlayerNativeActivity {
 
     public class Execute$$anonymous$$ethod extends TimerTask
     {
         public String uri;
         @Override
         public void run() {
             // TODO Auto-generated method stub
             Log.e("URLScheme","Running Uri::" + uri);
             UnityPlayer.UnitySend$$anonymous$$essage("!URLSchemeHandler", "OnOpenWithUrl", uri);
         }
         
     }
 
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         UrlResolver.currentContext = getApplicationContext();
         Intent intent = getIntent();
         if(intent != null && intent.getAction() == Intent.ACTION_VIEW)
         {
             Uri uri = intent.getData();
             if(uri != null)
             {
                 Timer t = new Timer();
                 Execute$$anonymous$$ethod mtd = new Execute$$anonymous$$ethod();
                 mtd.uri = uri.toString();
                 t.schedule(mtd, 1500);
             }
         }
     }
     
     @Override
     protected void onNewIntent(Intent intent)
     {
         Log.e("URLScheme", "onNewIntent");
         super.onNewIntent(intent);
         HandleURLIntent(intent);
     }
 
     protected boolean HandleURLIntent(Intent intent)
     {
         if(intent == null)
             return false;
         Uri uri = intent.getData();
         if(uri == null)
             return false;
         UnityPlayer.UnitySend$$anonymous$$essage("!URLSchemeHandler", "OnOpenWithUrl", uri.toString());
         return true;
     }
     
 }

It reacts to a URL scheme opening the app or focussing it if it is already open.

Show more comments

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

22 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

Related Questions

UnitySendMessage on Android 1 Answer

App in background still receive UnityPlayer.UnitySendMessage from Android plugin, normal ? 0 Answers

Is there any way to create a transparent view on Android? 0 Answers

Android SendMessage 1 Answer

Old camera view flickers for a couple of seconds when camera is changed 0 Answers


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