My app has two windows. First one is unity 3D exported to ios which uses vuforia kit for doing augmnted reality. Second is native xcode files. I need to pass a string value from unity to native objective c file because I need to display some values based on that. I'm using xcode 7 and unity 5. Please help me understand how to do it. Thanks in advance.
This can be done with the native plugin support in Unity. You need to import the native method into a class and call it via another method. You can find more documentation on it here: http://docs.unity3d.com/Manual/NativePlugins.html
Here is a really simple code example that should fit your needs.
The C# class:
using System.Runtime.InteropServices;
public static class SomeScript
{
[DllImport("__Internal")]
private static extern void _NativeMethodName(string stringValue);
public static void PassStringValue(string stringValue)
{
_NativeMethodName(stringValue);
}
}
And the native plugin code:
extern "C" {
void _NativeMethodName(const char* stringValue)
{
NSLog(#"Passed string value: %s", stringValue);
}
}
Related
I have a certain task where i want to pass a UIView to one of the SDK's in my iOS project.
Now, my project is a simple 2D game build in Unity 3d. I want to send the Panel in my Canvas to that SDK as UIView.
I am aware of iOS and Unity communication. It happens using as Char*.
I am not sure how should i access that GameObject Panel in my iOS code as UIView. I will be very thankful if i receive any help on this.
Above is one of solution that you can access from ios interface but before see this, read https://docs.unity3d.com/Manual/PluginsForIOS.html. it will help to understand how unity and native code can communicate.
iOS: test.mm
#import "Unity/UnityInterface.h"
...
...
-(void)SendMessage
{
UnitySendMessage("MessageGameCommunicator", "Receiver", [message UTF8String]);
}
Unity: Commnicator.cs, MessageGameCommunicator (GameObject name)
public class Commnicator : MonoBehaviour
{
public static void Receiver(string message)
{
// ...
// Do something.
}
}
After Unity interface get message from ios, you can implement whatever you want.
I developed Enterprise App, and installed it to my iPad.
The problem is that the iPad will be built in the wall, so it will not be possible to access the volume buttons.
I want to control the volume during the exhibition. I made UIButtons "-" and "+" to control the volume of the device instead of real volume button, not the Unity audio component's volume. I researched several times, so now what should I do in Unity?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class IOSManager : MonoBehaviour {
public static IOSManager _instance;
[DllImport("__Internal")]
private static extern void iosVolumeMinus();
private static extern void iosVolumePlus();
void Awake(){
_instance = this;
}
public void CallMinusFunc()
{
iosVolumeMinus();
}
public void CallPlusFunc()
{
iosVolumePlus();
}
}
Well, I tried somethings and realized that MPMusicPlayerController And MPVolumeView may work, but had some problems.
If I use MPMusicPlayerController I get
alert : 'volume' is deprecated:
first deprecated in iOS 7.0 - Use MPVolumeView for volume control.
But if I use MPVolumeView I get
alert : Incomplete definition of type 'struct
objc_class'.
It is my first time developing an iOS app, so I don't know what the problem is.
I am trying to create an iOS framework using swift. I follow this blog and also few others but the output is not the way I want.
Below is my original source file
public class TestClass: NSObject {
// publicVar description
public var publicVar: Int = 0
// doSomething description
public func doSomething() {
print("doSomething method")
}
}
After adding the framework into my project it's create an TestFramwork-Swift.h
You can see it's not contain my description. I want the framework
header files like Apple.
Can anyone help me to figure out this. I am not able to understand where I am doing wrong. I am also not able to add more swift files into my framework.
Just use triple slash instead of double slash for comments you want to show in your framework headers.
Is it possible to write unity IOS plugin in Swift?
I already have a working swift framework and want to use it as a plugin in Unity
I saw some places which say it can only be done on Objective-c but is there a workaround for swift?
How to call Unity methods
Unity interface functions are defined in UnityInterface.h in Xcode project built by Unity. This header file is imported in UnitySwift-Bridging-Header.h, so you can call the functions directly in your Swift codes.
To call Unity methods, use UnitySendMessage function like below:
// Example.swift
import Foundation
class Example : NSObject {
static func callUnityMethod(_ message: String) {
// Call a method on a specified GameObject.
UnitySendMessage("CallbackTarget", "OnCallFromSwift", message)
}
}
How to access Swift classes from Unity
Step 1: Create your Swift classes.
// Example.swift
import Foundation
class Example : NSObject {
static func swiftMethod(_ message: String) {
print("\(#function) is called with message: \(message)")
}
}
Step 2: Include "unityswift-Swift.h" and define C functions to wrap Swift classes in .mm file (Objective-C++).
// Example.mm
#import <Foundation/Foundation.h>
#import "unityswift-Swift.h" // Required
// This header file is generated automatically when Xcode build runs.
extern "C" {
void _ex_callSwiftMethod(const char *message) {
// You can access Swift classes directly here.
[Example swiftMethod:[NSString stringWithUTF8String:message]];
}
}
Step 3: Create interface class to call exported C functions from C#.
// Example.cs
using System.Runtime.InteropServices;
public class Example {
#if UNITY_IOS && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern void _ex_callSwiftMethod(string message);
#endif
// Use this method to call Example.swiftMethod() in Example.swift
// from other C# classes.
public static void CallSwiftMethod(string message) {
#if UNITY_IOS && !UNITY_EDITOR
_ex_callSwiftMethod(message);
#endif
}
}
Step 4: Call the method from your C# code.
Example.CallSwiftMethod("Hello, Swift!");
The file names of UnitySwift-Bridging-Header.h and unityswift-Swift.h are defined in "Objective-C Bridging Header" entry and "Objective-C Generated Interface Header Name" entry in Build Settings. These settings and other settings about Swift compiler are set automatically by PostProcesser when the Unity build runs.
Requirements
iOS 7 or later
Compatibility
Unity 5.3.5f1 Xcode 7.3.1
As top-level Swift is simply not accessible from Unity, the "workaround" for Swift is to write an Objective-C wrapper class around it, and access that.
Depending on the amount and complexity of your Swift code that might still be the most optimal approach.
We are using the Facebook SDK for Unity (v6.0) and I'd like to now whether there's a way that I can check if the Facebook app is installed on the device.
The reason is an existing bug in the Facebook SDK (see here: bug)
I want to identify this scenario (occurs only when the FB app is installed), and react accordingly.
"In order to use a native plugin you firstly need to write functions
in a C-based language to access whatever features you need and compile
them into a library. In Unity, you will also need to create a C#
script which calls functions in the native library."
from http://docs.unity3d.com/Manual/NativePlugins.html
So, basically you need to write your code in Objective-C and provide the communication between the Unity and the Native Code.
The code that you need to implement for checking Facebook APP is;
(void) checkFacebookApp
{
if ([[UIApplication sharedApplication] canOpenURL:[NSURLURLWithString:#"fb://"]])
{
return true;
}
}
However you need some communication between the Unity and Xcode project. So;
class SomeScript : MonoBehaviour {
#if UNITY_IPHONE || UNITY_XBOX360
// On iOS and Xbox 360 plugins are statically linked into
// the executable, so we have to use __Internal as the
// library name.
[DllImport ("__Internal")]
#else
// Other platforms load plugins dynamically, so pass the name
// of the plugin's dynamic library.
[DllImport ("PluginName")]
#endif
private static extern float checkFacebookApp ();
void Awake () {
// Calls the FooPluginFunction inside the plugin
// And prints 5 to the console
bool check = checkFacebookApp ();
}
}