Skip to content

IBridge interface

wvdvegt edited this page Apr 5, 2018 · 1 revision

This interface is empty and only used to enforce type safety.

Note: In Unity3D it’s not a good idea to derive the bridge from MonoBehaviour as in that case the constructor will run on a different thread and access to certain API methods is prohibited (specifically getting the user data directory).

Better is to add a MonoBehaviour property the bridge (this property can also be shared with IWebServiceRequest) and set it using a constructor taking a MonoBehaviour as parameter.

If the bridge where derived from MonoBehaviour, its Start() method would run on a different thread then the rest of the code and access to Application.persistentDataPath would be unsupported.

As the assets and bridge are most likely created in a game script class that is derived from MonoBehaviour, passing that class as parameter to the Bridge constructor is a better solution.

MonoBehaviour is necessary to gain access to the StartCoroutine() method that is used for making web requests with either the WWW or the more versatile UnityWebRequest class.

/// <summary>
/// A bridge.
/// </summary>
public class Bridge : IBridge, IWebServiceRequest, ILog
{
	private MonoBehaviour behaviour {
		get;
		set;
	}
    
	/// <summary>
	/// The storage dir for IDataStorage use. The folder will be located in the Unity Assets Folder.
	/// </summary>
	private static String StorageDir;
    
	public Bridge() : base()
	{
		// Debug.Log("Bridge Constructor");
	}
    	
	public Bridge(MonoBehaviour behaviour) : this()
	{
		// Debug.Log("Bridge Constructor (behaviour)");
		this.behaviour = behaviour;
		
		StorageDir = Path.Combine(Application.persistentDataPath, "DataStorage");
	}
    
	// More code
}

Clone this wiki locally