Twitter

Programming with C# and Unity 3D game engine – part 002 .

In this tutorials I will show how to deal with lists and enumerations.
First you need to make one C# script under Unity 3D. If don’t know how to do it then read this tutorial.
The list is an object which holds variables in a specific order.
The enumeration is an object with one distinct type that consists of a set of named constants called the enumerator list.
Let’s start with lists.
You need to put this on your script:

using System.Collections.Generic;

Now you can create list under your class , see the example with one class named YourClass:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class YourClass : MonoBehaviour 
{
	public List<int> ages = new List<int>();
	public List<string> names = new List<string>();
	public List<GameObject> objects List<GameObject>();
    void Start ()
    {

    }
}

We can see the List’s syntax is at first confusing. If you take a look to types – int, sting and GameObject and variables – ages, names and objects then the base remain this: List<...> … = new List<...>(); where types are into <> and variables can be created with = new … ().
Now will add one value to names with:

names.Add("myname");

To reverse one list in-place then use this:

names.Reverse();

If you want then can remove one:

names..Remove("myname");

We can use an array to initialize a List named animals:

List<string> animals = new List<string>()
	{
	    "cat",
	    "fox",
	    "wolf"
	};

Loop into the list animals and print the list:

foreach (int animal in animals)
	    Console.WriteLine(animal);
	}

To get the number of elements from animals list, use this:

animals.Count();

To clean the list just use this:

animals.Clean();

The next step is enumerations and is define by the enum type.
An enum type internally contains an enumerator list like this example:

public enum animals
    {
	cat,
	dog,
	fox,
	wolf
    };

In this enum named animals, cat will be zero (unless given another value explicitly), dog will be one, and fox will be two …
To print out the enum values, you can call ToString on the enum variable in a program.
The Console.WriteLine can automatically call the ToString method.
Using GetValues method with foreach to returns an array that contains a value for each member of the enumType enumeration.

foreach (var value in Enum.GetValues(typeof(animals))) {
...
}

Sometimes we have a string value that we want to convert to an equivalent enum and we can use Parse().
Also you can used many enum like: Enum.Parse, Enum.TryParse, Enum.Format, Enum.IsDefined…
You can get values from enum , see the example:

int oneanimal = (int)animals.cat;

Convert enums to list:

List listanimals = Enum.GetValues(typeof(animals)).Cast().ToList();

JavaScript – Processing.js 2D library – part 002.

In the last tutorials I told you about studio sketchpad.
Another way to working with processing is to run your script into html files.
If you want to download the processing then you can get it from processing website.
The download will come with an integrated development environment.
If you want to use your processing sketch then you need to make some changes because I will got errors with the IDE.
The changes I make to fix that is to put into the top of your sketch Ball ball001; and MovingBall mb001;.
See the source code.

Ball ball001;
MovingBall mb001;
class Ball{
    
    float x;
    float y;
    
    float speedX=1;
    float speedY=1;
    
    Ball(float _x, float _y){
        x = _x;
        y = _y;
    }
    void update(){
        x+=speedX;
        y+=speedY;
        }
    void display(){
        ellipse(x,y,20,20);
    }
}

class MovingBall extends Ball {
    MovingBall(float _x, float _y){
        super(_x,_y);
        }
    void drawline(){
        stroke(0,0,100);
        line(x,y,0,0);
        }
    }

void setup(){
     size(400,400);
     smooth();
     ball001 = new Ball(200,200);
     mb001 = new MovingBall(100,100);
}

void draw(){
    background(0);
    ball001.display();
    mb001.display();
    mb001.update();
    mb001.drawline();
}

The most of this features can be found here.

JavaScript – Processing.js 2D library – part 001.

This is part 001 from tutorials series about Processing.js.
The javascript library Processing help us to create one good interface with our javascript.
This library come with many features to show into canvas data and infos and also to create 2D and 3D interfaces.
I used to show this features one online editor named studio sketchpad and can be found here
Using this online editor can help you but if you want to use all features then you need to put the library into your html file.
In this tutorial I just use the online editor and later will come with another tutorials about how to set the library.
Make one account of this online editor if you want to save your work.
The development is simple and has some rules.
The script will have two functions setup – for settings and draw – basically will draw the canvas.

void setup(){
}
void draw(){
}

This will fill with functions, statements and all the commands available to Processing.js.
Take a look at page references.
On the bottom of the script can also make classes.
This will be created like this:


class my_class{
    
}

As can we see classes comes without void and args ().
Let’s how it’s works classes by using two : Ball and MovingBall.
If you want to make one class and then you need to used then use this steps:
– create the class Ball;
– add two functions: update and display;
– add also the possition of the class by using two float variables x,y ;
– because any class need to have one instance then you need one,so put this instance into the class and named like the class Ball;
– this instance will need to take the possition and then to used so you need also two variables: _x and _y;
– if you want to show it then also you need a display function with some 2D/3D primitives;
Also the classes can be created not just to show you the 2D/3D primitives.
Let’s see the source code of this class named Ball.


class Ball{
    
    float x;
    float y;
    
    
    Ball(float _x, float _y){
        x = _x;
        y = _y;
    }

    void display(){
        ellipse(x,y,20,20);
    }
}

Let’s make another class named MovingBall.
This class will be like the Ball class and will be extended by Ball class.
This means the all content of Ball class can be used or not by MovingBall.


class MovingBall extends Ball {
    MovingBall(float _x, float _y){
        super(_x,_y);
        }
    }

This MovingBall has the MovingBall extends the Ball with _x,_y and also used super to reference the superclass of a subclass. Take a look where is put the variables x,y and _x,_y into the Ball and MovingBall.
The Ball will get _x,_y possitions and will give it to x,y, also the MovingBall will follow the same rules.
Now I will add one function to MovingBall named drawline, see:


    void drawline(){
        stroke(0,0,100);
        line(x,y,0,0);
        }

This will set the sets the color used to draw lines and borders around shapes with stroke function and will make one line from x,y to 0,0.
When will run this function will have one line.
Let’s see the MovingBall class:

class MovingBall extends Ball {
    MovingBall(float _x, float _y){
        super(_x,_y);
        }
    void drawline(){
        stroke(0,0,100);
        line(x,y,0,0);
        }
    }

The first step when you want to using this library is to know how to set the canvas and how to change it.
The size of this canvas can be done with this function: size.
For example : size(400,400) means 400 pixels x 400 pixels.
The background can be change it with background() function and sets the color used for the background.
The smooth function will draw all geometry with smooth (anti-aliased) edges.
Because we want to draw something then you need to fill the setup and draw functions.
Also into setup function will put the new created classes and will used into draw function to draw.
Let’s see the final source code:

class Ball{
    
    float x;
    float y;
    
    float speedX=1;
    float speedY=1;
    
    Ball(float _x, float _y){
        x = _x;
        y = _y;
    }
    void update(){
        x+=speedX;
        y+=speedY;
        }
    void display(){
        ellipse(x,y,20,20);
    }
}

class MovingBall extends Ball {
    MovingBall(float _x, float _y){
        super(_x,_y);
        }
    void drawline(){
        stroke(0,0,100);
        line(x,y,0,0);
        }
    }

void setup(){
     size(400,400);
     smooth();
     ball001 = new Ball(200,200);
     mb001 = new MovingBall(100,100);
}

void draw(){
    background(0);
    ball001.display();
    mb001.display();
    mb001.update();
    mb001.drawline()
}

You can see my example here:

Android ABIs list application.

This application will show us about ABIs (Application Binary Interface support).
Basically get the information about CPU and ABIs.
You can read more about this at ARM website.
The result will come like this screenshot.
device-2016-01-26-161709
The main problem was the SDK: minSdkVersion 21 / targetSdkVersion 23 and the emulator with Nexus 6P API 23. That make me to increase the RAM – 1Gb. The hardware is old and working hard with this settings , but finally I make the screenshot.
Let’s see the source code of two files: MainActivity.java

package com.free_tutorials.catalin.abi_list;

import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView textSupportedABIs = (TextView)findViewById(R.id.supportedabis);

textSupportedABIs.setText(“Build.SUPPORTED_ABIS:\n”);
String[] SUPPORTED_ABIS = Build.SUPPORTED_ABIS;
for(String abi : SUPPORTED_ABIS){
textSupportedABIs.append(abi + “\n”);
}
textSupportedABIs.append(“\n”);

textSupportedABIs.append(“Build.SUPPORTED_32_BIT_ABIS:\n”);
String[] SUPPORTED_32_BIT_ABIS = Build.SUPPORTED_32_BIT_ABIS;
for(String abi32 : SUPPORTED_32_BIT_ABIS){
textSupportedABIs.append(abi32 + “\n”);
}
textSupportedABIs.append(“\n”);

textSupportedABIs.append(“Build.SUPPORTED_64_BIT_ABIS:\n”);
String[] SUPPORTED_64_BIT_ABIS = Build.SUPPORTED_64_BIT_ABIS;
for(String abi64 : SUPPORTED_64_BIT_ABIS){
textSupportedABIs.append(abi64 + “\n”);
}
textSupportedABIs.append(“\n”);
}
}

…and activity_main.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:padding=”16dp”
android:orientation=”vertical”
tools:context=”com.free_tutorials.catalin.abi_list.MainActivity”>

<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center_horizontal”
android:autoLink=”web”
android:text=”http://catalin-festila.blogspot.com”
android:textStyle=”bold” />

<TextView
android:id=”@+id/supportedabis”
android:layout_width=”match_parent”
android:layout_height=”wrap_content” />
</LinearLayout>

You can see the supportedabis is a string fill by MainActivity.java and show under activity_main.xml .
And that all about sources you need to make the aplication. All files you see is make by Android Studio 2.0. Also you need to have same function like java file and take a look at android manifest intro.

I used the default AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.free_tutorials.catalin.abi_list">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

PowerShell tips – part 002.

I will come with new examples and tips with Windows 10 PowerShell.
Let’s start:

List recursive android application from your folder using:
Get-ChildItem -Recurse -Filter “*.apk”

Calculate the number of archives for gmail attachment limit ( max gmail size 25MB):
132MB / 25MB

Lists all of the commands in the current PowerShell session.:
Get-Command

Show infos about powershell command shell:
Get-PSSnapin

Show all alias commands from powershell:
Get-Alias