Unity WebPlayer to Android Build Issues - android

I am new to Unity so a well stepped out answer would be nice. I am trying to make a dice roller on an Android platform. I was following this very well put together tutorial http://games.ucla.edu/resource/unity-1-beginner-tutorial-dice-making-pt-1/ (There is a second part too)
The problem is that it was made for a Web Player. If I try to build it for Android I get two particular errors.
I have two simple scripts with one error associated with each one.
SideTrigger.js - Error: BCE0019: 'currentValue' is not a member of 'UnityEngine.Component'.
public var faceValue = 0;
function OnTriggerEnter( other : Collider ) {
var dieGameObject = GameObject.Find("SixSidedDie");
var dieValueComponent = dieGameObject.GetComponent("DieValue");
dieValueComponent.currentValue = faceValue; //ERROR HERE
Debug.Log("Die1: " + faceValue);
}
DieValue.js - Error: BCE0019: 'text' is not a member of 'UnityEngine.Component'.
public var currentValue = 0;
function Update () {
var dieTextGameObject = GameObject.Find("DieText");
var textMeshComponent = dieTextGameObject.GetComponent("TextMesh");
textMeshComponent.text = currentValue.ToString(); //ERROR HERE
}
I'm assume it's purely a syntactical issue, but I can't seem to find a solution.

GetComponent using string is not recommended due to performance reasons, it is documented here.
It is better to use this:
var dieValueComponent : DieValue = dieGameObject.GetComponent(DieValue)
Or this:
var dieValueComponent : DieValue = dieGameObject.GetComponent.<DieValue>()
See if that works.

Related

How do I get the JSON result from Amadeus API (Kotlin Android)

I am trying to use the Amadeus Offers Search API with the following code:
when (val flightOffers = amadeus.shopping.flightOffersSearch.get(
originLocationCode = "MDZ",
destinationLocationCode = "MAD",
departureDate = LocalDate.parse("2020-11-11").toString(),
adults = 2,
max = 1
)) {
is ApiResult.Success -> {
if (flightOffers.succeeded) {
println("RESULT SUCCEEDED")
println(flightOffers.data)
}
else
{
println("RESULT DIDN'T SUCCEEDED")
}
}
is ApiResult.Error -> {
println("RESULT ERROR")
}
}
And if I compile that the logcat output is as follows:
I/System.out: RESULT SUCCEEDED
Which makes me think that flightOffers.data is empty.
However if I try this code:
val flightOffers = amadeus.shopping.flightOffersSearch.get(
originLocationCode = "MDZ",
destinationLocationCode = "MAD",
departureDate = LocalDate.parse("2020-11-11").toString(),
adults = 2,
max = 1
)
println("AMADEUS: $flightOffers")
I get the following output:
I/System.out: AMADEUS: Success(meta=Meta(count=1, links={self=https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=MDZ&destinationLocationCode=MAD&departureDate=2020-11-11&adults=2&max=1}), data=[FlightOfferSearch(type=flight-offer, id=1, source=GDS, instantTicketingRequired=false, nonHomogeneous=false, oneWay=false, lastTicketingDate=2020-05-03, numberOfBookableSeats=7, itineraries=[Itinerary(duration=PT18H, segments=[SearchSegment(departure=AirportInfo(iataCode=MDZ, terminal=null, at=2020-11-11T07:10:00), arrival=AirportInfo(iataCode=AEP, terminal=null, at=2020-11-11T08:45:00), carrierCode=AR, number=1403, aircraft=Aircraft(code=738), duration=PT1H35M, id=1, numberOfStops=0, blacklistedInEU=false, co2Emissions=null), SearchSegment(departure=AirportInfo(iataCode=EZE, terminal=A, at=2020-11-11T13:25:00), arrival=AirportInfo(iataCode=MAD, terminal=1, at=2020-11-12T05:10:00), carrierCode=UX, number=42, aircraft=Aircraft(code=789), duration=PT11H45M, id=2, numberOfStops=0, blacklistedInEU=false, co2Emissions=null)])], price=SearchPrice(currency=EUR, total=1151.26, base=510.0, fees=[Fee(amount=0.0, type=SUPPLIER), Fee(amount=0.0, type=TICKETING)], grandTotal=1151.26), pricingOptions=PricingOptions(includedCheckedBagsOnly=true, fareType=[PUBLISHED], corporateCodes=null, refundableFare=false, noRestrictionFare=false, noPenaltyFare=false), validatingAirlineCodes=[UX], travelerPricings=[TravelerPricing(travelerId=1, fareOption=STANDARD, travelerType=ADULT, price=SearchPrice(currency=EUR, total=575.63, base=255.0, fees=null, grandTotal=0.0), fareDetailsBySegment=[FareDetailsBySegment(segmentId=1, cabin=ECONOMY, fareBasis=ZYYOPO, segmentClass=Q, includedCheckedBags=IncludedCheckedBags(weight=0, weightUnit=null)), FareDetailsBySegment(segmentId=2, cabin=ECONOMY, fareBasis=ZYYOPO, segmentClass=Z, includedCheckedBags=IncludedCheckedBags(weight=0, weightUnit=null))]), TravelerPricing(travelerId=2, fareOption=STANDARD, travelerType=ADULT, price=SearchPrice(currency=EUR, total=575.63, base=255.0, fees=null, grandTotal=0.0), fareDetailsBySegment=[FareDetailsBySegment(segmentId=1, cabin=ECONOMY, fareBasis=ZYYOPO, segmentClass=Q, includedCheckedBags=IncludedCheckedBags(weight=0, weightUnit=null)), FareDetailsBySegment(segmentId=2, cabin=ECONOMY, fareBasis=ZYYOPO, segmentClass=Z, includedCheckedBags=IncludedCheckedBags(weight=0, weightUnit=null))])])], dictionaries={locations={MAD={cityCode=MAD, countryCode=ES}, EZE={cityCode=BUE, countryCode=AR}, MDZ={cityCode=MDZ, countryCode=AR}, AEP={cityCode=BUE, countryCode=AR}}, aircraft={789=BOEING 787-9, 738=BOEING 737-800}, currencies={EUR=EURO}, carriers={AR=AEROLINEAS ARGENTINAS, UX=AIR EUROPA}})
Which means that the API is returning a JSON but then I can't use flightOffers with gson to pass this data to a DataClass because flightOffers is a ApiResult> and I don't know how to use that. According to their library docs it should be done like I did it in the first try.
I appreciate all the help and advice I can get. This is my first Android App.
Nice to see that we have a new Android developer in the community !
So first, in Android you should avoid using println, instead you should use Log.d/e/w/i, this method will print your result in android logcat.
For what I see you successfully setup your project and where able to make query from the sdk.
In the android sdk, every get() will give you a correct data object and not just JSON. You don't have to take care of parsing the answer. The thing you have in your flightOffers.data is in fact a List<FlightOfferSearch> that you can use right away !

Unable to load font for use with ImageSharp in Xamarin Android app

I have a Xamarin Forms app where I've included a font file called Roboto-Regular.ttf in the Assets folder of the Android project. Its Build Action is set to AndroidAsset.
Using the SixLabors.Fonts NuGet package, I'm trying to load this font to use it for watermarking.
However, trying to install the font using the asset stream, an exception is thrown saying:
System.NotSupportedException: Specified method is not supported.
var fonts = new FontCollection();
FontFamily fontFamily;
using (var fontStream = Assets.Open("Roboto-Regular.ttf"))
{
fontFamily = fonts.Install(fontStream); // Fails with "method not supported"
}
return fontFamily;
Any ideas what might be causing this, or if there is a better way to load fonts for use with the SixLabors.ImageSharp package?
Edit: I tried the suggestion below by SushiHangover, but it yields the same result:
There is are two Assets.Open methods and one provides an accessMode (C# Access enum flag set):
using (var fontStream = Assets.Open("Roboto-Regular.ttf", Android.Content.Res.Access.Random))
{
fontFamily = fonts.Install(fontStream);
}
re: https://developer.android.com/reference/android/content/res/AssetManager.html#open(java.lang.String,%20int)
public enum Access
{
[IntDefinition (null, JniField = "android/content/res/AssetManager.ACCESS_BUFFER")]
Buffer = 3,
[IntDefinition (null, JniField = "android/content/res/AssetManager.ACCESS_RANDOM")]
Random = 1,
[IntDefinition (null, JniField = "android/content/res/AssetManager.ACCESS_STREAMING")]
Streaming,
[IntDefinition (null, JniField = "android/content/res/AssetManager.ACCESS_UNKNOWN")]
Unknown = 0
}
Seems the underlying Stream did not have Length or Position properties (which explains the exception), so for now I resorted to converting to a seekable MemoryStream instead:
using (var assetStreamReader = new StreamReader(Assets.Open("Roboto-Regular.ttf"))
{
using (var ms = new MemoryStream())
{
assetStreamReader.BaseStream.CopyTo(ms);
ms.Position = 0;
var fontFamily = new FontCollection().Install(ms);
}
}
Looking at the FontReader implementation, the error now makes even more sense: https://github.com/SixLabors/Fonts/blob/master/src/SixLabors.Fonts/FontReader.cs
However, I'm not sure why Assets doesn't return a seekable stream?

Accessing the DOM from a Firefox for Android add-on

I am attempting(and failing) to insert elements into every page visited. I am only able to insert elements into the initial page(the page displayed after the add-on has been installed). The code snippet below demonstrates this behaviour.
I have tried placing the logic that inserts the H1 element within onOpenWindow(), although that doesn't seem to make any difference.
NOTE: The code below is in addition to template boilerplate code
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import('resource://gre/modules/Services.jsm');
function loadIntoWindow(window) {
if (!window)
return;
window.NativeWindow.toast.show("Inserting script", "short");
var contentWindow = window.BrowserApp.selectedBrowser.contentWindow;
var document = contentWindow.document;
document.body.innerHTML = "<h1>THIS TEXT WAS INSERTED</h1>";
}
var DOMWindows = Services.wm.getEnumerator('navigator:browser');
while (DOMWindows.hasMoreElements()) {
var aDOMWindow = DOMWindows.getNext();
var allTabs = aDOMWindow.BrowserApp._tabs;
for (var i=0; i<allTabs.length; i++) {
var aContentWindow = allTabs[i].window;
}
}

Android 4.1 change - transition and webkittransition defiend, how to properly determine name of transition end event?

I have noticed that with the update from android 4.0 to 4.1, there is a change as to css transition prefix in the stock browser & webView
Basically, both "transition" and "webkitTrantion" are defiend.
Modernizr.prefixed("transition") returns webkitTrantion on android 4.0
Modernizr.prefixed("transition") returns trantion on android 4.1
However, as to transition end event name, "transitionend" event is not defined / does not work. you still need to use the webkit specific "webkitTransitionEnd" event name
QUESTION: I cannot find any documentation on this change, and thus are not certain how to proceed... can anyone shed some light on this? any suggestions or links to docs woudl be greatly appreciated!
TO REPRODUCE:
function whichTransitionEvent(){
var t;
var el = document.createElement('fakeelement');
var transitions = {
'OTransition':'oTransitionEnd',
'MSTransition':'msTransitionEnd',
'MozTransition':'transitionend',
'WebkitTransition':'webkitTransitionEnd',
'transition':'transitionEnd'
}
for(t in transitions){
if( el.style[t] !== undefined ){
alert (transitions[t]);
}
}
}
The code above, will result in just one popup showing up on android 4.0, and 2 popups for android 4.1 since on 4.1, both "transition" and "webkitTransition" as valid
I had a similar problem where Chrome on desktop and Android on Samsung devices would report another event name than what they actually used. The only way I can think of that would find what they are actually using is by triggering an event, set up several different event listeners, and see which one was triggered. The code below (from this gist) essentially does this and sets Modernizr.transitionEnd to be that eventname.
var $M = window.Modernizr
var _ = window._ // underscore.js
// Defines prefixed versions of the
// transitionEnd event handler
transitionFinishedEvents = {
'WebkitTransition' : 'webkitTransitionEnd',
'MozTransition' : 'transitionend',
'OTransition' : 'oTransitionEnd',
'msTransition' : 'msTransitionEnd',
'transition' : 'transitionEnd'
};
// Feature detect actual transitionEnd keyword by triggering an event
window.setTimeout(function () {
var div = document.createElement('div');
div.id = "my-transition-test";
div.style.position = 'absolute';
div.style.zIndex = -10;
div.style.bottom = '-1000px';
div.style.height = '100px';
div.style.width = '100px';
div.style.background = 'yellow';
div.style.display = 'hidden';
window.document.body.appendChild(div);
$('#my-transition-test').one(_.values(transitionFinishedEvents).join(" "), function (e) {
if ($M.transitionFinishedEvent !== e.type) {
window.console.warn("Changing misconfigured Modernizr.transitionFinishedEvent to " + e.type + ". (Was " + $M.transitionFinishedEvent + ")");
$M.transitionFinishedEvent = e.type;
}
window.document.body.removeChild(div);
});
window.setTimeout(function () {
div.style[$M.prefixed('transition')] = '0.1s';
div.style[$M.prefixed('transform')] = 'translate3d( 100px,0,0)';
}, 25);
}, 25);
Afterwards you can easily set up an event listener for transitionEnd that will work on all platforms (that has CSS3 transitions):
$("#fooBar").on($M.transitionEnd, function() {
// do something clever
}
The code has dependencies on underscore.js and jQuery, but can easily be turned into vanilla js.
Relevant links for people affected by this:
[Modernizr]Unprefixed version of 'transition' and 'transform' incorrectly returned for Android 4.1.1 (Samsung Note II)
[Leaflet] Freeze on Android 4.1.1 + Samsung Galaxy Note II

Issue accessing local xml files in apk file - Air for Android

This is my first time really getting my teeth into Air for Android so please forgive me if this issue has been covered already. If it has then I've been unable to find it.
So I have an application that loads and displays xml data.
In the app I've got code to check if wiFi or equivalent is available and if so then pull live xml file and if not then pull the local xml file that was packaged with the application.
The app works fine if I am pulling in the xml from the live url but not if pulling from local.
After doing some research I discovered that when pulling in local file then Air for Android works slightly differently. So I need to resolve the application directory.
I did this and still no joy.
After a bit more research I read some post's that said I should use fileStream()
Tried this and still nada :(
All the time whilst testing in Flash IDE it works as intended.
If I had any more hair left I would be pulling it out right now!
The local xml file is set in the "includes"
Sample code below I am using for testing
var subURL:String = "xml_feeds/myxmlfile.xml"
var fileStream:FileStream = new FileStream();
var file:File = File.applicationDirectory.resolvePath(subURL);
fileStream.addEventListener(Event.COMPLETE, processXMLData);
fileStream.openAsync(file, FileMode.READ);
MovieClip(parent).txStates.text = file.url+" - TRYING"
var prefsXML:XML = new XML()
function processXMLData(event:Event):void{
MovieClip(parent).txStates.text = file.url+" - OPEN"
prefsXML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
var tempArr:Array = new Array();
var reportCount:Number = prefsXML.row.column.length()
for (var i = 0; i < reportCount; i++) {
var rName:String = prefsXML.row.column[i].#name.toString();
var rValue:String = prefsXML.row.column[i].toString();
var rTitle:String = prefsXML.row.column[i].#name.toString()
tempArr.push([rName, rValue, rTitle]);
}
showData()
fileStream.close();
}
Is there anything I've missed?
UPDATE: 21/08/12
No idea what is going on with this. Here is the code i now have to use in order to load in the local xml file. Seems rather long winded
function listing():void{
var folders:Array = new Array();
folders = File.applicationDirectory.getDirectoryListing();
for(var i:Number =0;i<folders.length;i++){
if(folders[i].isDirectory){
if(folders[i].name=="xml_feeds"){
var files:Array = new Array();
files = folders[i].getDirectoryListing();
for(var j:Number=0;j<files.length;j++){
if(files[j].name=="CTSection2.xml"){
fileStream.openAsync(files[j], FileMode.READ);
fileStream.addEventListener(Event.COMPLETE, processXMLData);
fileStream.addEventListener(IOErrorEvent.IO_ERROR, localXMLFailLoad);
}
}
}
}
}
}

Categories

Resources