Skip to content

Instantly share code, notes, and snippets.

@robert-michels
Last active March 24, 2021 22:23
Show Gist options
  • Select an option

  • Save robert-michels/ecd16b13e1952fa3818c55f6d93eb5c5 to your computer and use it in GitHub Desktop.

Select an option

Save robert-michels/ecd16b13e1952fa3818c55f6d93eb5c5 to your computer and use it in GitHub Desktop.
// CODE SNIPPET, NOT THE FULL FILE //
void Start()
{
origTimeLeft = timeLeft;
timeLeft = timeLeft * 3;
imageObjectGOs = new GameObject[allImageObjects.Length];
ui_handler.dataTable = ui_handler.gimmeDatabase();
//check what data is available for targeted ads
ArrayList availableData = new ArrayList();
if (((string)ui_handler.dataTable["Drugs_consumed"]).Contains("Caffeine")) availableData.Add((Texture)coffeeAd);
if (((string)ui_handler.dataTable["Drugs_consumed"]).Contains("Cannabis")) availableData.Add((Texture)cannabisAd);
if (((string)ui_handler.dataTable["Drugs_consumed"]).Contains("Tobacco")) availableData.Add((Texture)tobaccoAd);
if (((string)ui_handler.dataTable["Favorite_Sportsbrands"]).Contains("Northface")) availableData.Add((Texture)northFaceAd);
if (((string)ui_handler.dataTable["Favorite_Sportsbrands"]).Contains("Puma")) availableData.Add((Texture)pumaAd);
//creat boolean array to see which ads should be targeted
bool[] replaceWithTargetedAd = new bool[allImageObjects.Length];
if (availableData.Count > 0)
{
//calculate how many targeted ads there should be in total
int numberOfAdsToTarget = mapNumber(availableData.Count, 1, 5, (int)allImageObjects.Length/6, (int)allImageObjects.Length / 2);
int amountOfIndexesSet = 0;
//randomly assign indexes of the random ads (so they are spread equally throughout the panels)
while (amountOfIndexesSet < numberOfAdsToTarget)
{
int randomVal = Random.Range((int)0, (int)replaceWithTargetedAd.Length);
if (!replaceWithTargetedAd[randomVal])
{
replaceWithTargetedAd[randomVal] = true;
amountOfIndexesSet++;
}
}
}
for (int i = 0; i < allImageObjects.Length; i++)
{
//load random image from the generic Ads pool, assign to current RawImage
if (replaceWithTargetedAd[i])
{
// -> targeted
allImageObjects[i].texture = (Texture)availableData[Random.Range(0, availableData.Count - 1)];
}
else {
// -> generic
allImageObjects[i].texture = genericAds[(int)(Random.Range(0, genericAds.Length - 1))];
}
//correct scale, so it has correct aspect ratio and it's scaled down
allImageObjects[i].SetNativeSize();
int w = (int)(allImageObjects[i].rectTransform.rect.width / 5);
int h = (int)(allImageObjects[i].rectTransform.rect.height / 5);
allImageObjects[i].rectTransform.sizeDelta = new Vector2(w, h);
//asign GameObjects for each component
imageObjectGOs[i] = allImageObjects[i].gameObject;
}
//shuffle Array, so images are loaded in random order
shuffleArray(imageObjectGOs);
for (int i = 0; i < imageObjectGOs.Length; i++)
{
//deactivate all ads
imageObjectGOs[i].SetActive(false);
}
//Start coroutine to begin showing ads
startTimer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment