Skip to content

Instantly share code, notes, and snippets.

@hkusoft
hkusoft / DynamicCanvasScaler.cs
Last active March 7, 2018 02:36
Dynamic scales Unity Canvas to make the UI resolution independent
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// Attach this script to objects that has a CanvasScaler component.
///
/// This makes sure the UI enlarges/shrinks when the device resolution changes.
///
@hkusoft
hkusoft / CSharpHttpGet.cs
Last active September 4, 2017 02:45
Generic function to run an HTTP Get API call
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Net.Http.Headers;
/// <summary>
/// This function fires an Http Get call and get the raw resoonse in string
/// </summary>
/// <param name="base_url">e.g. new Uri("https://api.mobile.azure.com/"); </param>
@hkusoft
hkusoft / Unity3DZoomFit.cs
Created September 2, 2017 15:09
Unity3D: Zoom Fit view a GameObject
internal static Bounds GetBound(GameObject go)
{
Bounds b = new Bounds(go.transform.position, Vector3.zero);
var rList = go.GetComponentsInChildren(typeof(Renderer));
foreach (Renderer r in rList)
{
b.Encapsulate(r.bounds);
}
return b;
}
@hkusoft
hkusoft / RunCommand.cs
Last active September 1, 2017 03:26
C#: Run command and get output
public static string RunCommand(string Command)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C " + Command; //e.g. Command = "dir"
p.StartInfo.CreateNoWindow = true; //important
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
@hkusoft
hkusoft / example_react_native.js
Last active May 17, 2017 02:31
Example template for React-Native JS file
var React = require('react');
var Foo = require('Foo');
/**
* General component description, for instance use below description
*
* The button component represents generic button UI control with
* an image and a title sting.
*
* props:
@hkusoft
hkusoft / ObjectToString.cs
Created October 19, 2016 09:26
C#: Serialize and Deserialize Objects to and from xml strings
public static string SerializeObject<T> (T objectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer (objectToSerialize.GetType ());
using (StringWriter textWriter = new StringWriter ()) {
xmlSerializer.Serialize (textWriter, objectToSerialize);
return textWriter.ToString ();
}
}
/**
* Get version number from build.gradle
* You might also use BuildConfig.VERSION_CODE to see if this works
* @return version code integer
*/
public static int getAppVersionCode() {
if (context == null) return -1;
PackageManager packageManager = context.getPackageManager();
int versionCode = -1;
try {
private static Activity getForegroundActivity(){
Activity activity = null;
try {
Class activityThreadClass = Class.forName("android.app.ActivityThread");
Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
activitiesField.setAccessible(true);
Map<Object, Object> activities = (Map<Object, Object>) activitiesField.get(activityThread);
if(activities == null)
// Example usage:
// new GetJsonTask().execute("https://api.stackexchange.com/2.2/info?site=stackoverflow&key=app key");
class GetJsonTask extends AsyncTask<String, Void, String> {
private Exception exception;
protected String doInBackground(String... urls) {
try {
Request request = new Request.Builder()
.url(urls[0])
AsyncTask.execute(new Runnable() {
@Override
public void run() {
//DoSth()
}
});