Creating Objects using Code

Creating Objects using Code
– This is called Instantiating
– You are creating a new instance of something

References
References allow to drag-and-drop objects into code using the Unity Editor.

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

	public GameObject objectToCreate;

	// Use this for initialization
	void Start () {
		// make an object
		Object.Instantiate(objectToCreate, new Vector3(2,4,6), Outereign.identity);
	}
	
	// Update is called once per frame
	void Update () {

	}
}

Update() Method

Update() Method
– called every frame
– For VR, this might be called 60, 90, or even 120 times per second.

This speed depends on hardware and a variety of software factors

Time.deltaTime
– Gives you smooth framerate-independent animation
– Contains the amount of time it took to render the previous frame
– Changes every frame

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (transform.position.y > 0.5f){
			transform.Translate (0, -2.5 * Time.daltaTime, 0, Space.World);
		}		
	}
}

unity manual
https://docs.unity3d.com/ja/current/Manual/index.html
scripting API
https://docs.unity3d.com/ja/current/ScriptReference/index.html

-Divided into namespaces.
-Namespaces establish a groupings between related objects
-Almost all scripts start with using UnityEngine;
-This gives you access to all the objects in the UnityEngine namespace

Camera.background-Color
https://docs.unity3d.com/ScriptReference/Camera-backgroundColor.html

Behaviors

A dog object might have
“bark” behavior
“walk around” behavior
“love humans” behavior

Scripts describe behaviors that get attached to Game Objects

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		transform.Translate (0, -2.5 * Time.daltaTime, 0, Space.World);
	}
}

the Fetch Request

fetch('<URL-to-the-resource-that-is-being-requested>');
fetch('https://api.unsplash.com/search/photos?page=1&query=flowers')

fetch('https://api.unsplash.com/search/photos?page=1&query=${searchedForText}', {
	method: 'POST'
});

[javascriit]
fetch(‘https://api.unsplash.com/search/photos?page=1&query${searchedForText}’, {
headers: {
Authorization: ‘Client-ID abc123’
}
}).then(function(response){
debugger;
});[/javascript]

Since the Unsplash API we’re using will return JSON to us, we just need to call .json() on the response variable.

fetch('https://api.unsplash.com/search/photos?page=1&query${searchedForText}', {
	headers: {
		Authorization: 'Client-ID abc123'
	}	
}).then(function(response){
	return response.json();
});
function addImage(data){
	let htmlContent = '';
	const firstImage = data.result[0];

	if (firstImage){
		htmlContent = '<figure>
			<img src="${firstImage.urls.small}" alt="${searchedForText}">
			<figcaption>${searchedForText} by ${firstImage.user.name}</figcaption>
			</figure>
	} else {
		htmlContent = 'Unfortunately, no image was returned for your search.'
	}

	responseContainer.insertAdjacentHTML('afterbegin', htmlContent);
}

Handling returning error

const imgRequest = new XMLHttpRequest();
imgRequest.onload = addImage;
imgRequest.open('GET', 'https://api.unsplash.com/photos?page=1&query=${searchedForText}');
imgRequest.setRequestHeader('Authorization', 'Client-ID <client-id-here>');
imgRequest.send();
function addImage(){
	const data = JSON.parse(this.responseText);
	const firstImage = datat.results[0];

	responseContainer.insertAdjacementHTML('afterbegin', <figure>
		<img src="${firstImage.urls.small}" alt="${searchedForText}">
		<figcaption>${searchedForText} by ${firstImage.user.name}</figcaption>
		</figure>
		);
}

jQuery ajax()

$.ajax(<url-to-fetch>,<a-configuration-object>);
$.ajax(<just a configuration object>);

var settings = {
	frosting: 'buttercream',
	colors: ['orange', 'blue'],
	layers: 2,
	isRound: true
};

const myDeliciousCake = MakeCake({
	frosting: 'buttercream',
	colors: ['orange', 'blue'],
	layers: 2,
	isRound: true
});

$.ajax({
	url: 'http://swapi.co/api/people/1/'
});
function handleResponse(data){
	console.log('the ajax request has finished!');
	console.log(data);
}

$.ajax({
	url: 'http://swapi.co/api/people/1/'
}).done(handleResponse);

A request

function handleSuccess(){
	const data = JSON.parse(this.responseText);
	const.log(data);
}
asyncRequestObject.onload = handleSuccess;

unsplash for developer
https://unsplash.com/developers
create an application
https://unsplash.com/oauth/applications

The New York Times for developers
https://developer.nytimes.com/

function addImage(){}
const searchedForText = 'hippos';
const unsplashRequest = new XMLHttpRequest();

unsplashRequest.open('GET', 'https://api.unsplash.com/search/photos?page=1&query=${searchedForText}');
unsplashRequest.onload = addImage;

unsplashRequest.send()
function addArticles(){}
const articleRequest = new XMLHttpRequest();
articleRequest.onload = addArticles;
articleRequest.open('GET', 'http://api.nytimes.com/svc/search/v2/articlesearch.json?q=${searchedForText}&api-key=<API-key-goes-here>');
articleRequest.send();

APIs

What’s an API?
The acronym “API” stands for:

Application
Programming
Interface

google API
https://developers.google.com/apis-explorer/#p/
ProgrammableWeb
https://www.programmableweb.com/apis/directory

const asyncRequestObject = new XMLHttpRequest();
asyncRequestObject.open('GET', 'https://unsplash.com');
const req = new XMLHttpRequest();
undefined
req.open('GET', 'https://www.google.com/');
undefined
VM453:1 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
function handleSuccess(){
	console.log(this.responseText);
}
asyncRequestObject.onload = handleSuccess;

function handleError(){
	// in the function, the 'this' value is the XHR object
	console.log('An error occured');
}
asyncRequestOjbect.onerror = handleError;
function handleSuccess (){
	console.log(this.responseText);
}
function handleError(){
	console.log('An error occurred')
}

const asyncRequestObject = new XMLHttpRequest();

asyncRequestObject.open('GET', 'https://unsplash.com');
asyncRequestObject.onload = handleSuccess;
asyncRequestOjbect.onerror = handleError;

asyncRequestObject.send();

Ajax with XHR

GET Request: An internet request for data. Sent from a client to a server.
Response: A server’s response to a request. Sent from a server to a client. A response to a GET request will usually include data that the client needs to load the page’s content.

-Ajax Definition
Asynchronous
JavaScript
And
XML
request

Get request and response
Data: XML, JSON, HTML

xhr(XMLHttpRequest (XHR)) is Asynchronous

Technical Documentation

OSS, JOB
documentation for you, coworkers, or your users!

README
app, bin, config, db, lib, log, public, test, vendor/assets, gitignore, README, Gemfile, Gemfile.lock

What happens if I don’t choose a license?
It’s super important to choose License.

Choose an open source license
which of the following best describes your situation?
https://choosealicense.com/

-add to README
known bugs
frequently asked questions
table of contents

Readable READMEs with Markdown
-> easily readable

Markdown is a light markup language often used for READMEs. It is fairly straightforward, and much of the syntax is intuitive.

#Here is your task
‘code’
**Here**
_there_

Markdown files should be saved with a .md extension.

<h1>My Fabulous Recipe</h1>
<p>This recipe for <strong>cereal and milk</strong> has been passed down my family for months.</p>
<h2>Ingredients</h2>
<ul>
	<li>Cereal (you can find cool cereals <a href="www.example.com/coolcereals">here</a>)</li>
	<li>Milk</li>
</ul>

<h2>Directions</h2>
<p>If I were writing these out as <em>code</em>, it might look something like this:</p>

<pre><code>if bowl is empty:
	add cereal
	if bowl only has cereal in it:
	add milk
</code></pre>
</body>