How to programmatically clear application data in unity - c#

I`ve been developing android game in unity3d. My app uses a lot of storage memory so I wanna clean it up after close (user presses the quit button and all cached memory gonna reset). It should work like the "Force Stop" on device. I wrote the method:
private void deleteData() {
String packageName = getApplicationContext().getPackageName();
Runtime runtime = Runtime.getRuntime();
runtime.exec(""+packageName);
I have following errors in unity editor:
The name `getApplicationContext' does not exist in the current context.
The type or namespace name `Runtime' could not be found. Are you missing an assembly reference?
I should import "Runtime" but I couldn`t find how to exactly do it in c# script. Is it possible to implement Android sdk methods in c# scripts? Thanks for help.

No need to make you proper module to do that, you can just delete cache using Caching.ClearCache(); inside Unity, it will delete all the cache created by the application. Also if you want to delete just the player preferences you can use PlayerPrefs.DeleteAll();

Related

Loading UI Toolkit assetbundle from Mod

I have written a mod for a video game that has a basic level of mod support, and provides a loader and access to game data etc. Currently the UI for the mod is using the IMGUI and I want to get rid of it.
I've been trying to write a proper UI in Unity, building an assetbundle but am struggling on getting my mod to actually load it.
As a POC I created the exact UI from the manual below, and the C# Script to build the bundle. This works, the manifest file lists my asset inside of the bundle.
https://docs.unity3d.com/Manual/UIE-HowTo-CreateRuntimeUI.html
https://docs.unity3d.com/540/Documentation/Manual/BuildingAssetBundles.html
Inside of the game when I try and load this asset from my mod at the most basic level I'm getting an error
Unable to read header from archive file: C:/Test/CustomAsset/customui.assets
Failed to read data for the AssetBundle
Script used to load test:
var bundleLoadRequest = AssetBundle.LoadFromFile(
Path.Combine("C:\\Test\\CustomAsset", "customui.assets"));
Log(bundleLoadRequest.GetAllAssetNames().ToString());
I know this path is valid because if I type an invalid path I get a different error "unable to locate asset" or something to that effect. If someone could please assist in pointing me in the right direction
Asset Bundles generally only work for the Unity version they are built with. Is it possible there is a mismatch between your editor version and the version the game you are modding was created with?
Sadly there is no Version check at the beginning that will let you know and they just won’t work.

The type or namespace name "..." could not be found - Unity Vuforia

I am pretty new to Unity and Vuforia. I am trying to do solve this following problem. I have a script file that I am using for 2 different Image targets. Only 1 script should be running at a time. So i opened "DefaultObserverEventHandler.cs" to handle tracking events. I want to disable the script on that particular Image target when the tracking is lost. But when i try to get a reference on the script "QuizzBehaviour" i get an error. "QuizzBehaviour" is located in my Assets folder. Thanks in advance.
Ok, I fixed it. I made my own "DefaultObserverEventHandler.cs" script and placed it in the same folder where my "QuizzBehaviour.cs" script is.
"Find" will search through objects in scene not in your folders.However, at all costs you should avoid searching for objects by typing their name manually.In your case, the matter is quite simple. You can either add a script to an object and then use the get component command, or you can make an abstract class and refer to it.

SteamAPI_Init() failed. Refer to Valve's documentation or the comment above this line for more information. | Steamworks.NET

[Steamworks.Net] SteamAPI_Init() failed. Refer to Valve's documentation or the comment above this line for more information.
It works fine in the Unity editor, but somehow I get an error with something built for Windows.
The code is below. Very simple.
if (SteamManager.Initialized)
{
string playerName = SteamFriends.GetPersonaName();
Debug.Log(playerName);
}
else
{
Application.Quit();
}
My username appears in debug in the editor. However, the build closes the app.
My app has not been published yet and I have not submitted any builds.
But, the fact that the editor can identify the user maybe means that something went wrong during the build process.
I created a steam_appid.txt at the root of the project and entered the AppID.
What's the difference between playing in the editor and playing a build?
Someone teach me please. Thank you. (Google Translated)
I don't know why, but when I submitted the build to Steamworks and installed it on Steam, I got no errors.
But uploading a build every time is very tedious...
I fixed it by placing the steam_appid.txt (containing the steam app id) next to the exported/built exe file
In your editor Unity. You must make sure:
App ID in the root folder(Asset(Not include inside Assets),,...,steam_appid.txt) config true with your app id created on a partner.
The steam app must run with runtime editor launching to confirm init.
--
I think your config and setup have error process. So I recommend you read more on 2 repositories:
https://github.com/rlabrecque/Steamworks.NET
https://github.com/Facepunch/Facepunch.Steamworks

Soundeffects not working. Could not load ... asset

So I'm trying to implement soundeffects whenever I click on an item. I've added a .wav sound to my Content folder.
Then I typed the following code in the Game1 class constructor:
SoundEffect soundEffect;
After that I wrote the following code in the Initialize function:
soundEffect = Content.Load<SoundEffect>("Laser_Shoot25.xnb");
Finally, I wrote the following code in my clickFunction:
soundEffect.Play();
When I tried running it, it gave me the following error.
Could not load nameOfSoundFile asset.
I've looked on Google to see if someone else had this same problem, but none of those solutions seem to work for me.
Does anyone know what else I'm supposed to do to make this work?
First thing that comes to mind is the possibility of you loading your assets before the Content object gets initialized. Please post the relevant code where you load your asset to know if this is the case (usually the initialize and LoadContent methods of your Game1 class).
The other possibility is the asset file format.
I'm not up to date with Monogame as I stopped using it with version 3.2, but if things remain the same on this aspect; then for working with sounds and spritefonts you need to add the asset as an exported xnb file, created by using the actual XNA content pipeline.
I have a blog post that explains exactly how to do all of this, which can be found here.
First, you need to install XNA on your machine (in the same post, it explains how to do it if you have Windows 8 and/or Visual Studio 2012/2013).
Then, just follow the steps detailed on the post:
So, once we have this, let’s create a new Windows Game project (it has
to be a game and not only a Content project as we will need to access
the game output directory).
After having this done, we can add our sound effects/instances to the
Content project by right clicking, selecting “Add existing item” and
choosing it in the folder explorer.
Before continuing, make sure you set the properties of the sound as
desired (in my case, I’ll be using a SoundEffect so I need to specify
it in the Content Processor field):
All that remains is to add the xnb file to the Content folder of your
Monogame project.

Read a file in AppController before the scene load

I am creating a game in Unity3d. I have some settings which are stored in a file.
Currently I have a scene which has a game object which reads these settings in C# and this the settings are available everywhere, however
I need these settings in
1. iPhone=AppController (didFinishLaunchingWithOptions)
2. Android = onCreate
I am not sure how to call C# methods in this condition so as to have settings available when they are needed in iPhone (AppController) or Android (OnCreate).
Can somebody tell me how it can be done?
Thanks,
Ashwani
In iOS in AppController.m you could call a C# method by using
UnitySendMessage("Game_Object_Script_or_Name", "MethodName","Message");
From Unity in your Game_Object_Script_or_Name object's js or script file add:
function MethodName(Message:string) {
}
This sends messages from XCode to Your Unity Binary. To do it vice versa is trickier.
You need to write a native plugin. See the following (the bottom part is in Mandarin):
http://www.cnblogs.com/pengyingh/articles/2340812.html

Categories