Saving Cubemap to file and load it in Unity - unity3d

I am trying to save cupemap object in unity and load it to perform a SAVE/LOAD function in my application so I tried this code to save my cubemap and it works fine :
foreach (var gameObj in FindObjectsOfType(typeof(GameObject)) as GameObject[])
{
if (gameObj.name.StartsWith("Chambre"))
{
PlayerPrefs.SetString("Chambre " + (l + 1), gameObj.name);
var chambre = new Material(Shader.Find("Skybox/Cubemap"));
chambre = gameObj.GetComponent<MeshRenderer>().material;
var Texture1 = chambre.GetTexture("_Tex");
if (!AssetDatabase.Contains(Texture1))
{
AssetDatabase.CreateAsset(Texture1, "Assets/Resources/Saved/chambre" + (l + 1) + ".mat");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
l++;
}
}
But the problem here that I use the import UnityEditor which is not accepted when you try to build the game. So is there another solution to save my cubemap and reload it without using "AssetDatabase.CreateAsset" because it called by UnityEditor?

I don't completely understand what you want to archive.
In a build there are no "Assets" anymore so the methods make no sence in a build.
To use them in the Editor anyway and be able to build your code you just have to use pre-processors #if UNITY_EDITOR <your code> #endif arround code blocks that shall only exist in the Editor:
#if UNITY_EDITOR
using UnityEditor;
#endif
...
#if UNITY_EDITOR
foreach (var gameObj in FindObjectsOfType(typeof(GameObject)) as GameObject[])
{
if (gameObj.name.StartsWith("Chambre"))
{
PlayerPrefs.SetString("Chambre " + (l + 1), gameObj.name);
var chambre = new Material(Shader.Find("Skybox/Cubemap"));
chambre = gameObj.GetComponent<MeshRenderer>().material;
var Texture1 = chambre.GetTexture("_Tex");
if (!AssetDatabase.Contains(Texture1))
{
AssetDatabase.CreateAsset(Texture1, "Assets/Resources/Saved/chambre" + (l + 1) + ".mat");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
l++;
}
}
#endif
So those codeblocks will not be included/executed in your build.

Related

How add code lines in Umazing unity plugin?

enter image description here
Since that the Unity Plugin mentioned above display only the Character into the Game Scene when I cliked the button DONE, I am trying to add code into UmazingCC.cs file to display both Character and the Name added in the tab to show both of them in the Game Scene.
The original UmazingCC part code is the following
enter code hereif (GUILayout.Button("Done", bottomBarButtonStyle)){
// Save the character, fade out the camera and load up the game scene
GameObject avatarGO;
if (selectedGender == "Man") {
avatarGO = maleAvatarSpawn;
} else {
avatarGO = femaleAvatarSpawn;
}
var avatar = avatarGO.GetComponent<UMAAvatarBase>();
if( avatar != null )
{
string finalPath = savePath + "/" + characterName + ".txt";
if (finalPath.Length != 0)
{
PersistentNameHolder.characterName = characterName;
var asset = ScriptableObject.CreateInstance<UMATextRecipe>();
asset.Save(avatar.umaData.umaRecipe, avatar.context);
System.IO.File.WriteAllText(finalPath, asset.recipeString);
ScriptableObject.Destroy(asset);
// If the camera has a fader, make it fade out and load the game scene, otherwise just load the scene ourselves
if (orbitCamera.GetComponent<CameraFader>() != null) {
orbitCamera.GetComponent<CameraFader>().fadeOutAndLoadScene(gameSceneName);
} else {
Application.LoadLevel(gameSceneName);
}
The Name metod code is the following
enter code here void FinishWindowContents (int windowID) {
GUILayout.BeginHorizontal(horizontalLayoutStyle);
GUILayout.BeginVertical(verticalLayoutStyle);
GUILayout.Label ("Nombre", bottomBarLabelStyle);
characterName = GUILayout.TextField(characterName);
GUILayout.EndVertical();
GUILayout.BeginVertical(verticalLayoutStyle);
if (characterName.Length == 0) {
GUI.enabled = false;
}
For it, Could anybody say me what code lines should I add in this place to show both Character and its name in the Game Scene?
Thanks in advance and sorry for my little english.
Ps. I added a screen image to see better the problem.

Coroutine loop freezes on unity3d run time

this is my unityScript code that have a simple coroutine loop. this Code works very well with editor test but on mobile phone and real time test the counter freezes if you want to go to main menu and come back again for counting down.
in other words you start from level 1=>2(every thing is run very well) after that 2=>1 and when you again go to 2 level count down will freeze at first number!!
function Start () {
StartCoroutine("DoSomething");
}
-
function DoSomething () {
for (var i = 5; i >= 0; --i) {
print("Future : \n" + i);
yield WaitForSeconds(1);
print("counting : \n " + i);
}
}
why should this code work at first time and freeze at second time?
You need to return finally the control from your yield, try this:
function DoSomething () {
for (var i = 5; i >= 0; --i) {
print("Future : \n" + i);
yield WaitForSeconds(1);
print("counting : \n " + i);
}
yield return null;
}

Add guitext in to gameobject

I am working on multiplayer collaboration in unity3d using smartfox server2x . In this there are 2 scripts
a)game manager.cs is drag in to game object and b)timer.js script is drag into guitext . How can i add this guitext into game object? When i am trying to do this iam getting an error like
"There is no 'GUIText' attached to the "Game" game object, but a
script is trying to access it.
You probably need to add a GUIText to the game object "Game". Or your
script needs to check if the component is attached before using it."
This is ma script
#pragma strict
public var timeLeft:float = 10.0f;
public var IsTiming : boolean;
//public bool IsTiming = false;
public function Start () {
//IsTiming = false;
}
function test1(vr1:boolean)
{
IsTiming=true;
Debug.Log("enter33333333333"+IsTiming);
//gamemanager = this.GetComponent("GameManager");
}
function OnGUI()
{
Debug.Log("enter2222222222"+IsTiming);
if(GUI.Button(Rect(0, 70, 150, 25), 'counter'))
{
IsTiming = true;
}
if(IsTiming)
{
Debug.Log("enterzzzzzzzzzz");
timeLeft -= Time.deltaTime;
if (timeLeft <= 0.0f)
{
guiText.text = "You ran out of time";
guiText.text = "Time is over";
}
else
{
guiText.text = "Time left = " + timeLeft + " seconds";
}
}
}
How can i add this script in to game object "Game".
The error will be happening because your game object does not have a GUIText component; you need to add one first.
To add a GUIText component, you could write gameObject.AddComponent(GUIText) within your Start function.
Or, you could add a GUIText component via the Unity editor menus by going Component > Add..., then type in GUIText.
Also, writing guiText in your script is shorthand for writing GetComponent(GUIText)!

Extract Unity Gameobject coordinates and export to text file

I have a level in which I have placed many instances of a prefab Gameobject (targets). I need to use the coordinates of those targets in a script. Is there a way to obtain the xyz vector coordinates of all those objects and export them to a text file? Right now I need to manually copy-past each individual target from the Unity inspector to MonoDevelop, which is a PITA...
To get the coordinates of an object use item.transform.Position where item is a reference to the object you want to get coordinates for. The result is a Vector3 from which you can do .x, .y or .z to get individual coordinates/
Writing to a text file is well-documented.
Alternatively, you may want to look into Serialization
EDIT: To do this for all objects in the scene:
string filePath = "D:/mycoords.txt";
string coordinates = "";
var objects = GameObject.FindObjectsOfType(Transform) as Transform[];
foreach(Transform trans in objects)
{
coordinates+= "x: " + trans.position.x.ToString() + ", y: " + trans.position.y.ToString() + ", z: " + trans.position.z.ToString() + System.Environment.NewLine;
}
//Write the coords to a file
System.IO.File.WriteAllText(filePath,coordinates);
NOTE: FindObjectsOfType will not return any assets (meshes, textures, prefabs, ...) or inactive objects.
EDIT3: If you want to only get your Targets, add a script to you Target prefab called "SaveMeToFile" and use this code:
string filePath = "D:/mycoords.txt";
string coordinates = "";
var objects = GameObject.FindObjectsOfType(SaveMeToFile) as SaveMeToFile[];
foreach(SaveMeToFile smtf in objects)
{
Transform trans = smtf.transform;
coordinates+= "x: " + trans.position.x.ToString() + ", y: " + trans.position.y.ToString() + ", z: " + trans.position.z.ToString() + System.Environment.NewLine;
}
//Write the coords to a file
System.IO.File.WriteAllText(filePath,coordinates);
EDIT 4: Or if you have any component specific to your target you can use that instead of SaveMeToFile in the code above, saving you from having to create a new, worthless Monobehaviour

Import existing animation file to unity web player model

I'm trying to import an existing .anim file to an existing unity model.
I know how do so it in Unity, and build into a HTML file, but I'm wondering is there a method to do it just using unityscript or javascript?
I mean using javascript to load an .anim file into the model in unity web player, if there is any solution, thanks!
Resource.Load allows you to do just that.
var mdl : GameObject = Resources.Load("Animations/"+ animationFolder+"/" + aName);
if (!mdl) {
Debug.LogError("Missing animation asset: Animations/" + animationFolder+"/"+aName + " could not be found.");
} else {
var aClip = mdl.animation.clip;
charAnimation.AddClip(aClip, aName);
Debug.Log(charAnimation[aName].name + " loaded from resource file " + animationFolder + "/" + aName + ". Length check: " + charAnimation[aName].length);
}