SoFunction
Updated on 2025-04-12

This is the example code to copy Unity mobile terminal

Preface

Unity official website provides detailed documentation and rich teaching videos. Yesterday, I followed the video to create a simple 3D game "roll-a-ball". The game involves a lot of basic knowledge about Unity, which is very good to get started.

This article mainly provides you with relevant content on Unity's mobile replication. We will share it for your reference and learning. I won't say much below. Let's take a look at the detailed introduction together.

The game has been online for a long time, and some players have gradually lost their money. In order to make the newly lost players return again, a recall function has been made! If a level 200 player fails to go online for 10 days and is successfully recalled, the recall player will be given a generous reward!

Q: So how to recall this lost player?

A: There must be something similar to the recall code when recalling, yes. The server generates a combination of letters and numbers of positioning numbers based on the player ID and sends them to the client. The client recall interface displays the recall code.

Q: It is impossible to let players enter one by one in the chat box, right? Players will definitely scold: "What a fool design, I won't play it anymore,"

A: The player is an old man, so it is still necessary to do the copy and recall function!

So how to implement this function? Does Unity support replication? Yes, Unity supports it, and it only supports PC side. This is the TextEditor!

TextEditor text = new TextEditor();
 = new GUIContent(yourText);
();
();

This enables the replication of the PC side. So how do mobile side do it? Don’t worry, young man, come one by one, let’s see how Android is copied first!

First, create an Android library project, add this code in MainActivity, export the jar package and call it in Unity. Next, I will talk about how to call it in Unity!

step:

1. Create the Plugins directory in the Assets directory of the Untiy3D project.

2. Create an Android directory under the Plugins directory.

3. Create a bin directory under the Android directory.

4. Place the jar package of the class you wrote in the bin directory

package ;
import com.;
import com.;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .;
import .;
import ;

public class MainActivity extends UnityPlayerActivity {
 private Vibrator mVibrator01;//Declare a vibrator object private static Context instance;
 private String TAG = "log";
 public static String gameObgectName = "Main Camera";
 public static String methodName = "OnCoderReturn";
 public static ClipboardManager clipboard = null;
 private BatteryListener listener;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  instance = getApplicationContext();
 }
 public static Context getContext()
 {
  return instance;
 }
  /*
   * Add text to the clipboard
   */
 public void copyTextToClipboard(final String str) 
 throws Exception { 
   if (() == null){ 
    (); 
   } 
   clipboard = (ClipboardManager) (Activity.CLIPBOARD_SERVICE); 
   ClipData textCd = ("data", str); 
   (textCd); 
 } 
 
 /*
   * Get text from the clipboard
   */
 public String getTextFromClipboard() {
  if (clipboard != null && ()
  && ().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
   ClipData cdText = ();
    item = (0);
   return ().toString();
  }
  return "null";
 }
}

How to call this code in Android?

/// <summary>
 /// Cut text /// </summary>
 /// <param name="input"></param>
 public void CopyToClipboard(string input)
 {
#if UNITY_ANDROID
  // Call to Android  AndroidJavaClass jc = new AndroidJavaClass("com.");
  AndroidJavaObject activity = <AndroidJavaObject>("currentActivity");

  if (activity == null)
   return;

  // Copy to clipboard  ("copyTextToClipboard", input);

  //Get text from the clipboard  string text = <string>("getTextFromClipboard");
#endif
 }

Android is better, and then it’s the high-end IOS. Some people become timid when they see IOS. They don’t know where to start or how to write. They have no idea. Sorry, I am as cowardly as you guys, and I am also a big girl in the sedan chair for the first time! Don’t be timid, just do it. At worst, you won’t succeed in the first time, just go to debug it! Having this mentality means you are still lucky. We are directly online projects and have no time to debug. When we finish, I have no idea! The super cowardly kind, later released the iPhone test package, but unexpectedly, once passed, the big stone in my heart finally fell! Let's see how IOS is implemented? The ObjectC and C languages ​​used by IOS are still somewhat similar. In OC, the .h file is a declaration and the .m file is an implementation, so it is necessary to have two files!

First declare this Clipboard class,

@ interface Clipboard : NSObject
extern "C"
{
  /* compare the namelist with system processes */
  void _copyTextToClipboard(const char *textList);
}
@end

Next is the file, remember not to forget to quote #import ""

#import""
@implementation Clipboard
//Copy text to IOS clipboard- (void)objc_copyTextToClipboard : (NSString*)text
{
  UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
   = text;
}
@end

extern "C" {
 static Clipboard *iosClipboard;

 void _copyTextToClipboard(const char *textList)
 { 
  NSString *text = [NSString stringWithUTF8String: textList] ;
  if(iosClipboard == NULL)
  {
   iosClipboard = [[Clipboard alloc] init];
  }
  [iosClipboard objc_copyTextToClipboard: text];
 }
}

.h files and .mm files need to be placed under any path under Assets/Plugins/ of the Unity project. Most programmers are obsessed with cleanliness and don’t put them under Assets/Plugins/IOS/ folders, and they feel uncomfortable! I'll say a few more words: Is there any difference between m and .mm? There is really, you can only write Object-C code in ".m", and C code in ".mm". So having extern "C" means we are using C code!

Unity3D calls to IOS classes

1. Create the Plugins directory in the Assets directory of the Untiy3D project.

2. Create an IOS directory under the Plugins directory.

3. Place the ".h" file and the ".mm" file in the IOS directory.

Okay, hurry up and talk about how to call it. It’s 1 o’clock at night, and it’s dawn if you talk too much! Just call the function declared by .h:

//The interface in the call_copyTextToClipboard (input); 
 ("CopyToClipboard_______"+input); 

Just call it like this, isn't you very disappointed? This is a call to all platforms

public static void CopyToClipboard(string input)
 {
#if UNITY_ANDROID
  // Call to Android  AndroidJavaClass jc = new AndroidJavaClass("com.");
  AndroidJavaObject activity = <AndroidJavaObject>("currentActivity");
  if (activity == null)
   return;

  // Copy to clipboard  ("copyTextToClipboard", input);
#elif UNITY_IPHONE
  //The interface in the call  _copyTextToClipboard (input); 
   ("CopyToClipboard_______"+input); 
#elif UNITY_EDITOR
  TextEditor te = new TextEditor();
   = new GUIContent(input);
  ();
  ();
#endif
 }

Okay, I’ve finished writing here. Now I feel that staying up late to write a blog is a luxury. Let’s go to bed! If you have any questions, please remember @ me and communicate with each other. The next article will write a plug-in to get the remaining storage space of your phone!

Portal:/wuzhang/ (Local download

Summarize

The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.