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
Related
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.
I'm using a custom logger(log4net) for Unity, following this guide. This involves making a custom appender that writes to Unity's console using Debug.Log(). All of this works great, however the problem is that whenever a log statement is printed it originates from the custom appender. So when I double click the message in the console I end up opening the code for the appender and not the source code from where the message originated. I want to print a clickable hyperlink in the console to the path of the source code.(I already have the path)
It seems the Unity console already supports clickable links:
I just don't know how to make it print links that I want.
In IDE's such as Intellij the console automatically parses links,so I tried to print the path for the source in the unity console for example:
(at Assets/Scripts/MovablePlatform.cs:)
But this didn't work
TL;DR
I want to know if it's possible to print clickable link in Unity's console such as 'http://www.google.com' or 'file:///D:/Mysourcode/main.cs'
Rich text supports anchor tags for file paths, but it doesn't appear to support URLs. Your example would be:
Debug.Log("Assets/Scripts/MovablePlatform.cs:7");
Logging a link like asset works for creating asset links since Unity 2019. This is the same mechanism used internally by Unity for clickable stack traces (also introduced in that version). The path must reference an existing Unity asset (not just any file).
Note, however, that while the link renders in blue in the main list of debug log messages, it isn't clickable there, only in the detailed pane below (which also shows the stack trace). That is, you must first click the log message, then click the link in the detailed message pane.
(Finally, in my testing I occasionally hit what may be a Unity bug or a bug in the Rider IDE integration, where I sometimes have to Alt+Tab away from Unity and back before it will open any asset link, stack trace links included. I assume doing so resets some sort of state in Unity's asset database, which is why it fixes things.)
Since Unity 2021.2.0a11, both web links and file links are supported:
Debug.Log("Click me: local file");
Debug.Log("Click me: website");
You can now even hook into the hyperlink handling and do whatever you please with the result using EditorGUI.hyperLinkClicked.
UnityEditor.EditorGUI.hyperLinkClicked += (window, args)
=> Debug.Log($"clicked link to {args.hyperLinkData["href"]} in {window}");
so you can even implement your own ad-hoc links (by using other attributes than href, to avoid clashes).
As far as I know, no.
Those links are there so that you can jump to any of the calling methods in the stack trace that resulted in the Debug.Log call, and all you have to do is scroll down. Unity added this feature precisely because of log appenders but the system is entirely managed by Unity. Double-click is still going to go to the actual Debug.Log line, but you can still access other lines via those "hyperlinks" but they are built automagically by Unity and there is no API for writing those links yourself.
If you don't want your log appender to lose that information, don't do whatever it is that is making Unity lose that information when you use your log appender. You didn't include your code, so why the extra information is being lost is unclear. Also check your console settings and that you are using Unity 2019.1 or newer.
An open source Unity Console plug-in is recommended to solve this problem. Click console-hyperlinks!
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();
i currently try to extend the unity editor with custom EditorWindow implementations. It follows loosely this guide.
I try to save a asset containing serialized objects as a sort of database.
That works just fine!
However. If i close unity and reopen it
db = (FigureDB)AssetDatabase.LoadAssetAtPath("Assets/Logic/Database/FigureDB.asset", typeof(FigureDB));
doesnt load the asset file unless i manually reimport the EditorWindow implementation within unity (rightclick on cs-file -> reimport).
I code in Visual Studio (whether this matters or not.... i suspected a line-endings-problem, but it doesnt seem to be the case)
After reopening Unity i also see within the inspector (while selecting the asset) The associated script can not be loaded. Please fix any compile errors and assign a valid script (even though there are no compile time errors visible)
Any suggestions? If more info is necessary i will gladly provide them, however it doesnt seem to be a code problem but rather an IDE-problem.
Do you had use this?
AssetDatabase.ImportAsset("Assets/Logic/Database/FigureDB.asset", ImportAssetOptions.Default);
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.