Cannot load scene from asset bundle in standalone - unity3d

Two scenes I am switching between both are from asset bundles used by this code. The code is working in Editor but not in the build. What I am missing?
public IEnumerator LoadSceneBundle(string assetBundleSceneName, string orignialName) {
//url = "file://" + Application.dataPath + "/AssetBundles/" + assetBundleSceneName + ".unity3d";
Debug.Log(Application.dataPath);
//this code for build
newExtractedPath = Application.dataPath;
//this code for runtime
//newExtractedPath = Application.dataPath.Substring(0, Application.dataPath.Length - 7);
Debug.Log(newExtractedPath +" :: newExtractedPath");
//url = "file://" + Application.dataPath + "/AssetBundles/" + assetBundleSceneName + ".unity3d";
url = "file://" + newExtractedPath + "/AssetBundles/" + assetBundleSceneName + ".unity3d";
Debug.Log("scene load url : " + url);
using (WWW www = WWW.LoadFromCacheOrDownload(url,1)){
yield return www;
if (www.error != null) {
throw new Exception("WWW download had an error : " + url + " " + www.error);
//Debug.Log("");
}
AssetBundle ab = www.assetBundle;
Debug.Log(www.assetBundle.mainAsset);
ab.LoadAll();
Application.LoadLevel(originalName);
ab.Unload(false);
}
}
After deployment, I made an AssetsBundle folder and placed my assetbundle scene files, but it isn't working. All was in vain.

Your path seems wrong. If you are embedding the asset bundles into your game, you should put them under the StreamingAssets folder so they'll end up in the game build. According to the documentation:
Any files placed in a folder called StreamingAssets in a Unity project will be copied verbatim to a particular folder on the target machine.
So do that and use Application.streamingAssetsPath to form your path and you should be able to use WWW.LoadFromCacheOrDownload to get them.

If you want to load an asset bundle for a standalone Unity Application, I think you would be better served by AssetBundle.CreateFromFile() - which is simpler and faster. Only catch is that the asset bundle can't be compressed.
http://docs.unity3d.com/ScriptReference/AssetBundle.CreateFromFile.html

Related

Xamarin - image saved to gallery without date

I use this code to save images to the gallery:
Uri uri = new Uri(baseUrl + imageName);
var img = await ImageService.Instance.LoadUrl(baseUrl + "social/social_" + imageName).AsJPGStreamAsync(quality:100);
string fileName = "Social_" + uri.ToString().Split('/').Last();
DependencyService.Get<IMediaService>().SaveImageFromStream(img, fileName);
await DisplayAlert("Saved", "Image saved to gallery!", "Ok");
the problem is, that the images do not have a time in the file, and are stored randomly in the gallery...
How can I add date to the files, so they are saved in the proper order in the gallery?
Just append something like this
DateTime.Now.ToString("MMddyyyyhhmmss");
string fileName = "Social_" + uri.ToString().Split('/').Last() + DateTime.Now.ToString("MMddyyyyhhmmss");
//03312020071656

How to properly integrate Google Sign-In into my Unity Android App build

private string text;
void Start()
{
PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().RequestIdToken().RequestServerAuthCode(false).Build();
text = "config created";
PlayGamesPlatform.InitializeInstance(config);
text = text + "\n" + "config initialized";
PlayGamesPlatform.Activate();
text = text + "\n" + "activated";
SignInWithPlayGames();
text = text + "\n" + "attempted to sign in";
}
public void SignInWithPlayGames()
{
UnityEngine.Social.localUser.Authenticate((bool success) =>
{
if (success)
{
string authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
text = text + "\n" + "Auth code is: " + authCode;
if (string.IsNullOrEmpty(authCode))
{
text = text + "\n" + "Signed into Play Games Services but failed to get the server auth code.";
return;
}
}
if (!success)
{
text = text + "\n" + "Failed to Sign into Play Games Services.";
return;
}
});
}
When I run this on Unity, I got
config created
config initialized
activate
Failed to Sign into Play Games Services.
attempted to signed in
which is fine since I am using my PC to test it. But I got an interesting result after I run my app on a real device. I got this:
config created
config initialized
activate
attempted to signed in
I think it skip if (success) from public void SignInWithPlayGames() method. My app never shows Google Play UI and I am not sure now if I am using the right code.
I've struggled quite a bit with Google Play in the last few weeks. I did not Retrieve the ID token or auth token in the builder however. It might be causing you problems.
Also, you need to cast the social user into a google user to access its properties.
I got it to work with the following code. (If you do not get it to work with this code, the problem resides in your settings in the googleplay Plugin and/or google play console.)
PlayGamesClientConfiguration config = new
PlayGamesClientConfiguration.Builder()
.Build();
// Enable debugging output (recommended)
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.Activate();
Social.localUser.Authenticate((bool success) =>
{
var googleUser = ((PlayGamesLocalUser)Social.localUser);
if (googleUser.authenticated)
{
// access googleUser properties and store them or use them
}
}

Unity webview loading webpage locally/by storing cache

I am trying to build a unity project, in my project i have to show the web page in app using unity webview either by loading locally or by once storing cache of that web page. i have tried loading locally by downloading a web page but without internet the content of the html files loaded but CSS, JS and Images are not. for loading locally i have pasted the folders and HTML file to a folder in project-> Assests-> Streaming Assets. Image for folders and HTML file is Streaming Assets directory Image the underlined folders and file is used. And in unity editor link for the file is provided like this:- Unity Editor Image and script used to load the file is below
case RuntimePlatform.OSXEditor:
case RuntimePlatform.OSXPlayer:
case RuntimePlatform.IPhonePlayer:
case RuntimePlatform.Android:
if (Url.StartsWith("http")) {
webViewObject.LoadURL(Url.Replace(" ", "%20"));
} else {
webViewObject.LoadURL("StreamingAssets/" + Url.Replace(" ", "%20"));
var src = System.IO.Path.Combine(Application.streamingAssetsPath, Url);
var dst = System.IO.Path.Combine(Application.persistentDataPath, Url);
var result = "";
if (src.Contains("://")) {
var www = new WWW(src);
yield return www;
result = www.text;
} else {
result = System.IO.File.ReadAllText(src);
}
System.IO.File.WriteAllText(dst, result);
webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
}
if (Application.platform != RuntimePlatform.Android) {
webViewObject.EvaluateJS(
"window.addEventListener('load', function() {" +
" window.Unity = {" +
" call:function(msg) {" +
" var iframe = document.createElement('IFRAME');" +
" iframe.setAttribute('src', 'unity:' + msg);" +
" document.documentElement.appendChild(iframe);" +
" iframe.parentNode.removeChild(iframe);" +
" iframe = null;" +
" }" +
" }" +
"}, false);");
}
break;
#else
If anyone Knows Please help.

How to load image from folder but not resource folder

I put my image at /Assets/Test/Textures/Player but without success my code is:
IEnumerator loadTexture(string videotype) {
if(videotype.Equals("3d")) {
WWW www = new WWW("file:///Assets/Meta1/Textures/Player/3D_foucs.png");
yield return www;
texture3D.mainTexture = www.texture;
} else if(videotype.Equals("2d") ){
} else if(videotype.Equals("360")) {
}
}
Place your png file in Resources folder of your project and load it using:
Texture2D _texture = Resources.Load("3D_foucs.png") as Texture2D;
EDIT :
If you really want to avoid using Resources.Load() then you can use Application.dataPath to access assets folder. But make sure you read the docs before that.
P.S : WWW is usually good for loading stuff from outside of project.
E.g from Application's Persistent Data or from web server.
Hope it helps

How to save a pdf file in iPhone memory and make it accessible by pdf file opener applications using titanium

i am new bie to titanium in my app i need to download and save a pdf file in iOS using titanium and the downloaded file can be accessible by third party pdf viewer applications.any help to achieve this thanks in advance.
1- Make sure your webservice returns a PDF file.
2- Use the FileSystem to manage your files
var xhr = Titanium.Network.createHTTPClient({
onload : function() {
var f = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, 'file.pdf');
f.write(this.responseData);
tmpFile = Ti.Filesystem.createTempFile();
var newPath = tmpFile.nativePath + "." + f.extension();
Ti.API.info("newPath: " + newPath);
tmpFile = Ti.Filesystem.getFile(newPath);
tmpFile.write(f.read());
},
timeout : 10000
});
xhr.open('GET', 'your webservice');
xhr.send();
Now you use a pdf viewer and open the PDF from the externalStorageDirectory
I tested on Android, it works !