Thursday, 9 August 2018

Unity 2017.2 package manager error

In the 2017.2 Unity version sometimes an error about unity package can happen.
In this case, when you create a new project or when you try to open one, it processes for 10 second then it shows this "unity package manager error"


Unity Package Manager Error


Failed to start unity package manager: operation timed out.please see the editor.log file for more information.

This error is related to a proxy server or a filtered internet connection.

Possible solutions are:

Use an updated version of Unity
Bypass the proxy server using a direct internet connection
Setup a proxy server from the command line

on windows systems, add these variables to the system

HTTP_proxy= <http_proxy>:port
HTTPS_proxy= <https_proxy>:port

The problem is about the Package Manager that can't connect outside the network.
Share:

Thursday, 3 August 2017

Proxy problems with Unity3D on Windows

When you're using Unity3D behind a proxy server maybe you did run into several problems:


  • registration problems
  • problems about access to the Asset Store
  • problems about download an asset from the store
In my case I was running into a "download problem" from the Asset Store.

Solution

  1. close Unity3D
  2. add these environment variables to your system
        HTTP_proxy= <http_proxy>:port
        HTTPS_proxy= <https_proxy>:port
      
        i.e.
        HTTP_proxy=http://myproxy.mydomain:8080
        HTTPS_proxy=https://proxy.mydomain:8080
  3. restart Unity3D
This should do the trick.

Share:

Thursday, 15 December 2016

How to make a Cheat Menu for testers

In this post I will show you how to create a simple cheat menu in your Unity game.


A simple cheat menu for Unity

What is this for?


With a cheat menu you let your testers to switch on hidden functions and let them to test the game with "unilimited lives" or "unlimited time".

How it works

An hidden menu can be obtained with a single CS script file: when the player/tester digits the "secret pass", the script will show the hidden menu. With it you can simply turn on a "trainer game", give the player infinite weapons or energy and so on...

Let's create it

  • open Unity
  • open a project (or create a new one)
  • create a new empty object in your scene
  • create a new script, let's call it "HiddenMenu.CS"
  • attach the script to your empty object

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class HiddenMenu: MonoBehaviour {

 private string[] cheatCode;
 private int index;
 private bool cheat;

 void OnGUI() {
  if (cheat) {
   // draw the hidden menu
   GUI.Box(new Rect(20, 80, 240, 240), "Cheat Menu :-) ");
   if (GUI.Button(new Rect(40, 120, 200, 20), "Reset all data")) {
    // do a total reset
   }
   if (GUI.Button(new Rect(40, 150, 200, 20), "Reset Levels")) {
    // do a reset all levels
   }
   if (GUI.Button(new Rect(40, 180, 200, 20), "Unlock Levels")) {
    // if you have blocked levels, here you can unlock them
    // for the testers
   }
   if (GUI.Button(new Rect(40, 210, 200, 20), "Unlimited credits")) {
    // give unlimited CREDITS to the player
   }
   if (GUI.Button(new Rect(40, 240, 200, 20), "Unlimited time")) {
    // give unlimited TIME to the player
   }
   if (GUI.Button(new Rect(40, 270, 200, 20), "Close this menu")) {
    cheat = false;
    index = 0;
   }
  }
 }

 void Start() {
  // The pass Code is "secret", user needs to input 
  // this word to show the hidden menu
  // You can change it whatever you want
  cheatCode = new string[] {
   "s",
   "e",
   "c",
   "r",
   "e",
   "t"
  };
  index = 0;
 }

 void Update() {
  if (Input.GetKeyDown(KeyCode.Escape)) {
   Application.Quit();
  }

  if (Input.anyKeyDown) {
   if (Input.GetKeyDown(cheatCode[index])) {
    // right input, check next digit
    index++;
   } else {
    // wrong input, restart from index 0
    index = 0;
   }
  }

  if (index == cheatCode.Length) {
   // cheat is ok, the hidden menu is now visible :-)
   cheat = true;
   index = 0;
  }
 }
}

Let's try it !

Run your Unity game and digit your pass-code (in this example the pass-code is "secret"). You now should see the hidden menu.

Create a simple new empty object in your scene


Your cute hidden menu in Unity


Configure your preferred pass-code and create your custom methods.

And enjoy!




Share:

Wednesday, 1 June 2016

Lesson learned: How to load resources dynamically at runtime and not loose your sanity on iOS

Even though I tinker and experiment with Unity3d since one year and half, I never crossed path with the necessity of loading assets dynamically at runtime.

At first I fell with the simple approach of System.IO methods that fooled me enough to skip all the documentation about the matter and made all work flawlessly in the editor.

 

SYSTEM.IO methods used:

// check if resource exists
System.IO.File.Exists(fileName)

// custom method to count png in a specified folder
int CountPNGinDirectoryPath(string dirPath){
   string[] totalFiles = System.IO.Directory.GetFiles(path);
   int counter = 0;
   foreach (string item in totalFiles){
      if (item.EndsWith(".png")){
         counter++;
      }
   }
   return counter;
}

 

Nice job!
Lets build it for iOS!
One second later: ERROR
 

Resource folder: why and how does it work

After some research I found out what I was missing:
"Resources"

From Unity's documentation:
The Resources class allows you to find and access Objects including assets.
[...]
All assets in the "Resources" folders will be included in a build.



First you need to create one or multiple folders named exactly "Resources", and place all assets needing manipulation at runtime under that folder (or subfolders).


Keep in mind that you need to use a case sensitive path in your methods and use "/" instead of "\" even if you are on windows, and that the Resources folder will be your root.

You also need to exclude file extension, since the resource will be packed with unity's proprietary format.

For example image.png placed in Background folder under Resources will be called with "Background/image" 



Resource class counterpart methods I used:

 

myTexture = (Texture2D)Resources.Load(resourceString, typeof(Texture2D));
// check if file exists - Resource.Load is null if resource does not exists
if (!myTexture) {
   // create 2d texture to fill the sprite
   myTexture = new Texture2D(1,1);
}

int CountPNGinDirectoryPath(string dirPath){
   int counter = 0;
   Object[] maps = Resources.LoadAll(dirPath);
   counter = maps.Length;
   Resources.UnloadUnusedAssets();
   return counter;
}
Share:

Thursday, 19 May 2016

Unity - How to use VSCode with Unity 5



Visual Studio Code is the last entry made by Microsoft and it remembers editor like
Both of them great editors.
 
Visual Studio Code has several good aspects:


  • It's free
  • It's lightness (It's an Editor not an IDE)
  • bring multiplatform versions (linux, windows and MacOSX)
  • C# syntax highlighning (with code completion)
  • Has a built-in GIT support (nice feature)
  • It can be used in a Unity Project (instead of Visual Studio or MonoDevelop)
 Here you can see a Unity session with VSCode.



To install and configure VSCode for Unity, follow these steps:

Install Visual Studio Code

  • Go here https://code.visualstudio.com/
  • Download your platform version
  • Install it
  • Install omnisharp: open Visual Studio Code, Quick Open (Ctrl+P) and digit "ext install omnisharp" then press enter to install it

Install VSCode/Unity Plugin:

  • Download the plugin via GIT or ZIP file git clone https://github.com/dotBunny/VSCode.git

How Integrate VSCode with Unity with plugin

  1. Open Unity
  2. Create a New Projecy (or open an existing one)
  3. Unzip (or copy) plugin into your asset directory (copy the Plugins\Editor\dotBunny directory to your project)
  4. To turn on the plugin, you will need to go to Unity Preferences and select the VSCode tab
  5. Open VSCode and point to the ASSET directory
  6. Enjoy !!!

Reference:

Visual Code Studio Home
VSCode Github
VSCode and Unity


Share:

Friday, 12 February 2016

Unity Android - Make Screenshot and share it

- Update 2016-05-04 -

We can write Java code inside Unity script by using the AndroidJavaObject class and the Call method.
So, it's easy to implement a "share screenshot" feature in your Unity Android Game.
The steps are:
  1. Save the actual screen to a Texture2D
  2. Save this texture to file (android filesystem)
  3. Create a new Intent class with ACTION_SEND action
  4. Execute this intent from the main Activity
This code could be inserted in a IEnumerator function.
Code Example:

using UnityEngine;
using System.Collections;
using System.IO;

/**
*  Share a Screenshot on Android (on Unity 5.3.4)
* 
*  Last update 2016-05-04
* 
*/
public class Screenshot : MonoBehaviour {

void OnGUI () {
    GUI.Box (new Rect (0,Screen.height - 100,220,70), "Action Menu");
    if(GUI.Button(new Rect(20,Screen.height-60,180,20), "Share Screenshot")) {
        StartCoroutine(ShareScreenshot());
    }
}

public IEnumerator ShareScreenshot() {
    
    // wait for graphics to render
    yield return new WaitForEndOfFrame();
    
    // prepare texture with Screen and save it
    Texture2D texture = new Texture2D(Screen.width, Screen.height,TextureFormat.RGB24,true);
    texture.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height),0,0);
    texture.Apply();
    
    // save to persistentDataPath File
    byte[] data = texture.EncodeToPNG();        
    string destination = Path.Combine(Application.persistentDataPath, 
                                      System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".png");        
    File.WriteAllBytes(destination, data);
    
    if(!Application.isEditor)
    {
        #if UNITY_ANDROID
        string body = "Body of message to be shared";
        string subject = "Subject of message";

        AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
        AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
        intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
        AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
        AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse","file://" + destination);
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), body );
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), subject);
        intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");
        AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
        
        // run intent from the current Activity
        currentActivity.Call("startActivity", intentObject);
        #endif
    }
}
}

Remember to set the "writeaccess" flag to TRUE.
You find it in: Edit -> Project -> Player -> Android Settings


Simple (but complete) Example


  1. Create a new scene
  2. Create an Empty Object (or use existing one)
  3. Attach the following script:

using UnityEngine;
using System.Collections;
using System.IO;

/**
*  Share a Screenshot on Android (on Unity 5.3.4)
* 
*  Last update 2016-05-04
* 
*/
public class Screenshot : MonoBehaviour {

void OnGUI () {
    GUI.Box (new Rect (0,Screen.height - 100,220,70), "Action Menu");
    if(GUI.Button(new Rect(20,Screen.height-60,180,20), "Share Screenshot")) {
        StartCoroutine(ShareScreenshot());
    }
}

public IEnumerator ShareScreenshot() {
    
    // wait for graphics to render
    yield return new WaitForEndOfFrame();
    
    // prepare texture with Screen and save it
    Texture2D texture = new Texture2D(Screen.width, Screen.height,TextureFormat.RGB24,true);
    texture.ReadPixels(new Rect(0f, 0f, Screen.width, Screen.height),0,0);
    texture.Apply();
    
    // save to persistentDataPath File
    byte[] data = texture.EncodeToPNG();        
    string destination = Path.Combine(Application.persistentDataPath, 
                                      System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".png");        
    File.WriteAllBytes(destination, data);
    
    if(!Application.isEditor)
    {
        #if UNITY_ANDROID
        string body = "Body of message to be shared";
        string subject = "Subject of message";

        AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent");
        AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");
        intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND"));
        AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
        AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse","file://" + destination);
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject);
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_TEXT"), body );
        intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_SUBJECT"), subject);
        intentObject.Call<AndroidJavaObject>("setType", "image/jpeg");
        AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
        
        // run intent from the current Activity
        currentActivity.Call("startActivity", intentObject);
        #endif
    }
}
}

Share:

Wednesday, 25 November 2015

Saturday, 31 October 2015

Wednesday, 23 September 2015

Monday, 21 September 2015

How to use the PAUSE MODE in your game

Another little problem when you're developing your first "bestseller" game of the time, is how to put it in pause mode.
With unity you can easly archive this, by setting the Time.scale to zero: the play game time is but all the Unity GUI objects are interactable while the rest of Unity freezes meanwhile.

You can quit the "pause mode" by setting the Time.timeScale to the original value (1)


Share:

Sunday, 20 September 2015